美文网首页iOS Developer
【iOS】UISegmentedControl实现切换按钮

【iOS】UISegmentedControl实现切换按钮

作者: 清無 | 来源:发表于2017-07-31 17:52 被阅读2799次
最终效果
实现:
-(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;
}

相关文章

网友评论

    本文标题:【iOS】UISegmentedControl实现切换按钮

    本文链接:https://www.haomeiwen.com/subject/rzprlxtx.html