ios - 内部触摸时用颜色填充 UIButton

标签 ios uibutton uicolor

这个问题在这里已经有了答案:





iOS - Setting UIButton background color only colors the corners

(3 个回答)


8年前关闭。




我想在 UIButton 被触摸时用颜色填充它。该方法可以在action方法中实现

- (IBAction)bocClick:(UIButton *)sender {
    [sender setBackgroundColor:[UIColor blackColor]];
}

我得到如下按钮

enter image description here

这个背景不足以满足我的需要,我需要有效果,比如..

enter image description here

最佳答案

您正在使用一轮 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];
}

输出是:-

enter image description here

类似的东西:-

iOS - Setting UIButton background color only colors the corners

关于ios - 内部触摸时用颜色填充 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18379656/

相关文章:

iphone - UI颜色比较

ios - UITextKit 中英文字符的顶部与行的顶部不对齐

iphone - 有没有办法找到设置了 AspectFit 的 UIImageView 内图像的调整大小高度/宽度?

ios - 从 Windows 移动到 mac 后,M1 上的 Flutter : GoogleMapsBase & Flutter. h 未发现错误

ios - 带有 titleLabel 的 Swift 菱形 UIButton

ios - UITableViewCell 不保存自定义按钮的状态

objective-c - UITextField 边框动画

ios - 在 iOS 中搜索自定义对象

ios - 如何使用 Swift 以编程方式将 UIButton 中的文本左对齐,图像右对齐?

ios - CNContactViewController导航条各版本不同