IOS开发路上的故事键盘上的鼓手iOS学习笔记OS

UISegmentedControl使用方法

2015-07-27  本文已影响22244人  c_xiaoqiang
  1. 初始化的方法

     //一次性初始化多个control,items可以是图片或者是文字array,系统会自动控制每个单元的大小
     //也可通过正常View的初始化进行
     - (instancetype)initWithItems:(NSArray *)items; 
    
  2. 事件添加

     //通过UIControl的方法来设置,当改变segments的时候通过事件UIControlEventValueChanged,
     //通过action来处理事件
     [self.mySegmentedControl addTarget:self
                         action:@selector(myAction:)
               forControlEvents:UIControlEventValueChanged];
    
  3. Segmented Controls的内容设置

     //通过索引设置以及获取Title和Image          
     - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
     - (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;
     - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
     - (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
    
  4. Segmented Controls Segments的设置

     //在索引点插入一个新的标题的segment
     - (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
     //在索引点插入一个新的图片的segment
     - (void)insertSegmentWithImage:(UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated;
     - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
     - (void)removeAllSegmens;
     //Segment的数量
     @property(nonatomic,readonly) NSUInteger numberOfSegments;  
     //被选中的索引
     @property(nonatomic) NSInteger selectedSegmentIndex;
    
  5. Segmented Controls显示和行为设置

     //默认值为NO,即点击后保持选中状态,如果设置为YES则,只是短暂的显示选中状态
     @property(nonatomic,getter=isMomentary) BOOL momentary;
     //通过索引调整每个segment的width,如果设置为0.0则表示自动调整大小,默认为0.0
     - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
     - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
     //通过索引调整文字或者图片在segment的offset,默认为(0,0)
     - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;
     - (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;
     //设置segment是否能选中
     - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;       
     - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;
     //是否根据内容的大小自动调整宽度
     @property(nonatomic) BOOL apportionsSegmentWidthsByContent NS_AVAILABLE_IOS(5_0);
    
  6. 自定义的外观

     //设置UISegmentedControl的边框,分隔线,文字,点击后的颜色
     @property(nonatomic,retain) UIColor *tintColor;
     //获取和设置背景图片,通过UIControlState和barMetrics的进行设置和获取,主要需要先设置
     //UIControlStateNormal才会有效果
     - (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
     - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
     //设置分割线的图片样式,首先需要设置UIControlStateNormal不管左边还是右边,
     //设置完成后再调用设置方法,根据UIControlState显示分割线
     - (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics
     - (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetric      //设置Title的样式,包括 font, text color,  shadow。通过NSAttributedString.h里面的键值对
     - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
     - (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
     //调整title和image的位置,通过给定的segment 
     - (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
     - (UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
    
上一篇下一篇

猜你喜欢

热点阅读