美文网首页
iOS UIButtonTypeSystem

iOS UIButtonTypeSystem

作者: pigLily | 来源:发表于2022-01-28 18:51 被阅读0次

通过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];
}

相关文章

网友评论

      本文标题:iOS UIButtonTypeSystem

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