Sha*_*man 7 colors uibutton ios
是否可以设置禁用 的背景颜色UIButton?当按钮被禁用时,我们想要使用更柔和的颜色,但我没有看到任何方法UIButton允许这样做.是否有解决方法或其他方式可以实现此目的?
我专注于你的评论 - "当按钮被禁用时,我们想要使用更柔和的颜色".如果更改按钮的alpha是可接受的解决方案,您可以很好地使用此代码.
someButton.alpha = 0.5;//This code should be written in the place where the button is disabled.
Run Code Online (Sandbox Code Playgroud)
老实说,使用按钮的最佳方式是子类化和自定义 UI,例如:
class XYButton: UIButton {
override public var isEnabled: Bool {
didSet {
if self.isEnabled {
self.backgroundColor = self.backgroundColor?.withAlphaComponent(1.0)
} else {
self.backgroundColor = self.backgroundColor?.withAlphaComponent(0.5)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以XYButton在故事板/xib 中或以编程方式使用。
** 更优雅:
didSet {
self.backgroundColor = self.backgroundColor?.withAlphaComponent(isEnabled ? 1.0 : 0.5)
}
Run Code Online (Sandbox Code Playgroud)
小智 5
似乎这是不可能的,因为背景颜色属性是由 的UIView父类控制的UIButton。
所以UIButton控制状态不拥有这个属性并且不控制它。
但你仍然可以继承UIButton并覆盖setEnabled方法来相应地更新颜色。
这是最明确的方法。因为您可以将继承的类分配给UIButtonStoryboard 或 Xib 文件中的任何一个。无需任何连接。(不IBOutlets)
希望这对您有帮助,如果您希望我可以分享一些代码。
希望这可以帮助 :
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 35)];
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[btn setBackgroundImage:image forState:UIControlStateDisabled];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15109 次 |
| 最近记录: |