swift - UIBarButtonItem UIButton视图alpha不起作用

时间:2016-10-02 01:33:36

标签: swift 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工作。

1 个答案:

答案 0 :(得分:0)

不确定为什么会这样,但我添加了这个,我们现在很好。

 override func viewWillAppear(_ animated: Bool) {

        button?.alpha = 0.4
    }

出于某种原因,将它放在viewWillAppear中会使得加载时alpha为0.4。

相关问题