iOS UIButtonTypeSystem

2022-01-28  本文已影响0人  pigLily

通过UIButtonTypeSystem创建的UIButton,在设置UIButton的选中状态时,会看到一个默认的蓝色背景。如图所示:


图片.png

可以通过设置UIButton的tintColor来修改这个背景颜色

    UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [testBtn setTitle:@"测试" forState:UIControlStateNormal];
    testBtn.frame = CGRectMake(20, 300, [UIScreen mainScreen].bounds.size.width - 40, 200);
    testBtn.tintColor = [UIColor whiteColor];
    [testBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [testBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected | UIControlStateHighlighted];

    self.testBtn = testBtn;
    [testBtn addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testBtn];
//点击测试按钮时,设置该按钮的selected为YES
- (void)testAction:(UIButton *)sender {
    sender.selected = YES;
}
//点击另外一个按钮,设置testBtn的selected属性为NO。
- (IBAction)clickAction:(UIButton *)sender {
    [self.testBtn setSelected:NO];
}
上一篇 下一篇

猜你喜欢

热点阅读