【iOS】UISegmentedControl实现切换按钮

最终效果
实现:
-(UISegmentedControl *)segment{
    if (!_segment) {
        UISegmentedControl *s = [[UISegmentedControl alloc] initWithItems:@[@"全部",@"已完成",@"待付款",@"待收货",@"待评价"]];
        s.backgroundColor = [UIColor whiteColor];
        s.selectedSegmentIndex = 0;
        [s addTarget:self action:@selector(actionValueChanged:) forControlEvents:UIControlEventValueChanged];
        
// 设置文字样式
        [s setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal]; //正常
        [s setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateHighlighted]; //按下
        [s setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor HYcolorWithHexString:@"#26b8f2"]} forState:UIControlStateSelected]; //选中
        
// 设置背景图
        [s setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [s setBackgroundImage:[UIImage new] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; 

// 设置分割线图
        [s setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal nilrightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];//
        [s setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        
        _segment = s;
    }
    return _segment;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容