iOS Developer

【iOS】UISegmentedControl实现切换按钮

2017-07-31  本文已影响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;
}
上一篇 下一篇

猜你喜欢

热点阅读