这个问题在这里已经有了答案:
iOS - Setting UIButton background color only colors the corners
(3 个回答)
8年前关闭。
我想在 UIButton 被触摸时用颜色填充它。该方法可以在action方法中实现
- (IBAction)bocClick:(UIButton *)sender {
[sender setBackgroundColor:[UIColor blackColor]];
}
我得到如下按钮
这个背景不足以满足我的需要,我需要有效果,比如..
最佳答案
您正在使用一轮 rect
自定义有点复杂的按钮。
您可以创建自定义类型按钮并使用背景图像,或创建自定义类型按钮并圆角并设置背景colour
自己的图层。
或者您也可以使用以下方法:-
您可以像下面的示例一样添加 UIView 或 Lable:-
- (IBAction)bocClick:(UIButton *)sender {
UILabel *lbl2=[[UILabel alloc]initWithFrame:_btn.bounds];
lbl2.text=@"Button";
lbl2.textAlignment=UITextAlignmentCenter;
lbl2.font=[UIFont boldSystemFontOfSize:16];
lbl2.backgroundColor=[UIColor redColor];
lbl2.layer.cornerRadius = 5;
lbl2.layer.masksToBounds = YES;
[sender addSubview:lbl2];
}
输出是:-
类似的东西:-
iOS - Setting UIButton background color only colors the corners
关于ios - 内部触摸时用颜色填充 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18379656/