迅速不工作
问题描述:
我创建与下面的代码扩展一个UIButton的UIBarButtonItem的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自定义视图:
UIBarButtonItem(customView: self.customButton!)
该按钮将alpha设置为0.4,但该按钮仍然显示为完整的alpha值!
我甚至试图在调用customButton方法后切换它,它根本不起作用。
我试图让按钮在第一次打开时显得被禁用,并且不可能让该alpha工作。
答
不知道为什么会发生这种情况,但我补充说,现在我们很好。
override func viewWillAppear(_ animated: Bool) {
button?.alpha = 0.4
}
由于某种原因,将它放在viewWillAppear中使得它的alpha值为0.4。