UISegmentedControl设置样式(圆角,边框颜色)


UISegmentedControl *segmentedControl = [[UISegmentedControl allocinitWithItems:@[NSLocalizedString(@"gtasks"nil),NSLocalizedString(@"question_hotel"nil)]];

        segmentedControl.layer.cornerRadius = 20;

    segmentedControl.clipsToBounds = YES;

    segmentedControl.tintColor = IWColor(85155219);

    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],

                                             NSForegroundColorAttributeName: [UIColor whiteColor]};

    [segmentedControl setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性

    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],

                                               NSForegroundColorAttributeName: [UIColor whiteColor]};

    [segmentedControl setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];

    [segmentedControl setBackgroundImage:[UIImage createImageWithColor:IWColor(5991154)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [segmentedControl setBackgroundImage:[UIImage createImageWithColor:IWColor(85155219)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    segmentedControl.apportionsSegmentWidthsByContent = NO;

    self.navigationItem.titleView = segmentedControl;

 

 

UISegmentedControl用法详解

2016.04.06 11:06* 字数 423 阅读 13726评论 2喜欢 7

SegmentedControl又被称作分段控制器

初始化方法:传入的数组可以是字符串也可以是UIImage对象的图片数组

// items can be NSStrings or UIImages. control is automatically sized to fit content

- (instancetype)initWithItems:(NSArray *)items;

设置控件风格:

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

注意:这个属性已经废弃,它的枚举如下:

//"The segmentedControlStyle property no longer has any effect") __TVOS_PROHIBITED;

typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {

    UISegmentedControlStylePlain,     // large plain

    UISegmentedControlStyleBordered,  // large bordered

    UISegmentedControlStyleBar,       // small button/nav bar style. tintable

    UISegmentedControlStyleBezeled,   // DEPRECATED. Do not use this style.

} NS_DEPRECATED_IOS(2_0, 7_0, 

设置是否保持选中状态:默认是NO,如果设置为YES,点击结束后将不保持选中状态.

@property(nonatomic,getter=isMomentary) BOOL momentary; 

获取标签个数:(只读)

@property(nonatomic,readonly) NSUInteger numberOfSegments;

设置标签宽度是否随内容自适应:如果设置为NO,则所有标签宽度一致,为最大宽度

@property(nonatomic) BOOL apportionsSegmentWidthsByContent;

插入文字标签在index位置:

- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated

插入图片标签在index位置

- (void)insertSegmentWithImage:(UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated

根据索引删除标签

- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;

删除所有标签

- (void)removeAllSegments;

重设标签标题

- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment; 

获取标签标题

- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment;

设置标签图片

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment; 

获取标签图片:标题的图片只能设置一个

- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;

根据索引设置相应标签宽度:如果设置为0.0,则为自适应,默认为此设置.

- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment; 

根据索引获取标签宽度

- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;

设置标签内容的偏移量:这个偏移量指的是 标签的文字或者图片

- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;

根据索引获取变标签内容的偏移量

- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment;

根据所以设置标签是否有效(默认有效)

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; 

根据索引获取当前标签是否有效

- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment;

设置和获取当前选中的标签索引

@property(nonatomic) NSInteger selectedSegmentIndex;

设置标签风格颜色:这个风格颜色会影响标签的文字和图片,如果设置为clearColor,那么整个segmentedControl将不会有背景和边框

@property(nonatomic,retain) UIColor *tintColor;

设置特定状态下segment的背景图案 UIBarMetrics是一个枚举,如下:(defaulf风格会充满背景)

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

```

 

```

typedef NS_ENUM(NSInteger, UIBarMetrics) {

    UIBarMetricsDefault,

    UIBarMetricsCompact,

    UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar

    UIBarMetricsCompactPrompt,

    UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact,

    UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt,

};

```

获取背景图案

```

- (UIImage *)backgroundImageForState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

```

设置标签之间分割线的图案

```

- (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

```

获取标签之间分割线的图案

```

- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

```

通过Attribute字符串属性字典设置标签标题

```

- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state

```

获取Attribute字符串属性字典

```

- (NSDictionary *)titleTextAttributesForState:(UIControlState)state

```

自行设置标签内容的偏移量

```

- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics 

```

注意:UIOffset为偏移量,这个结构体中有两个浮点数,分别表示水平量和竖直量;UISegmentedControlSegment类型参数是一个枚举,如下:

```objc

typedef NS_ENUM(NSInteger, UISegmentedControlSegment) {

    UISegmentedControlSegmentAny = 0,//所有标签都受影响

    UISegmentedControlSegmentLeft = 1,   //只有左边部分受到影响  The capped, leftmost segment. Only applies when numSegments > 1.

    UISegmentedControlSegmentCenter = 2, // 只有中间部分受到影响Any segment between the left and rightmost segments. Only applies when numSegments > 2.

    UISegmentedControlSegmentRight = 3,  //只有右边部分受到影响 The capped,rightmost segment. Only applies when numSegments > 1.

    UISegmentedControlSegmentAlone = 4,  //在只有一个标签的时候生效 The standalone segment, capped on both ends. Only applies when numSegments = 1.

};

```

添加点击事件:这里的时间是valueChanged

```

[segmentedControl addTarget:self action:@selector(sementedControlClick:) forControlEvents:UIControlEventValueChanged];

```

用segmentedControl和一个UIView简单封装一个选择控制器,作为导航控制器的titleView

 

![有背景.gif](//upload-images.jianshu.io/upload_images/1295463-7c2eee327aadd8e6.gif?imageMogr2/auto-orient/strip)

去掉背景之后

![没有背景.gif](//upload-images.jianshu.io/upload_images/1295463-2b8595bb5a6ac19a.gif?imageMogr2/auto-orient/strip)

 

![xib加载设置好约束](//upload-images.jianshu.io/upload_images/1295463-15b73d216cf12a22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 

![修改tintColor为clearColor](//upload-images.jianshu.io/upload_images/1295463-fe0f860df1ac5fbd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

 

```

#import "GZDFriendTrendTitleView.h"

@interface  GZDFriendTrendTitleView()

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

//底部线

@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

 

@implementation GZDFriendTrendTitleView

//初始化方法

+ (instancetype)friendTrendTitleView {

 

    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

}

//一次性设置

- (void)awakeFromNib {

   

    [self.segmentedControl setTitle:@"关注" forSegmentAtIndex:0];

    [self.segmentedControl setTitle:@"订阅" forSegmentAtIndex:1];

//设置普通状态下(未选中)状态下的文字颜色和字体

    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];

    //设置选中状态下的文字颜色和字体

    [self.segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];

//添加监听

    [self.segmentedControl addTarget:self action:@selector(sementedControlClick) forControlEvents:UIControlEventValueChanged];

}

//监听方法

- (void)sementedControlClick{

    //修改底部线的frame值产生动画

    [UIView animateWithDuration:0.25 animations:^{

        self.bottomView.centerX = (self.segmentedControl.selectedSegmentIndex == 0)? self.segmentedControl.centerX * 0.5 : self.segmentedControl.centerX * 1.5;

    }];

    

}

 

```

 

 

 

 

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM