我正在使用以下代码在扩展中创建一个 uibutton:
extension UIButton {
func customButton(_ title: String, selector: Selector) -> UIButton{
let button = UIButton()
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.titleLabel!.font = UIFont.systemFont(ofSize: 16)
button.setTitle(title, for: UIControlState())
button.setTitleColor(UIColor.black, for: UIControlState())
button.setTitleColor(UIColor(r: 220, g: 220, b:220), for: .highlighted)
button.titleEdgeInsets = UIEdgeInsetsMake(8, 8, 8, 8)
button.layer.cornerRadius = 5
button.sizeToFit()
button.addTarget(self, action: selector, for: .touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
button.alpha = 0.4
return button
}
}
问题是我将此按钮用作 UIBarButtonItem 中的自定义 View :
UIBarButtonItem(customView: self.customButton!)
该按钮将 Alpha 设置为 0.4,但该按钮仍然显示完整的 Alpha!
我什至尝试在调用 customButton 方法后在类中切换它,但它根本不起作用。
我试图让按钮在第一次打开时看起来被禁用,并且不可能让该 alpha 工作。
最佳答案
不知道为什么会发生这种情况,但我添加了这个,现在就很好了。
override func viewWillAppear(_ animated: Bool) {
button?.alpha = 0.4
}
出于某种原因,将其放入 viewWillAppear 中会使加载时的 alpha 值为 0.4。
关于swift - UIBarButtonItem UIButton View alpha 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813051/