【问题标题】:swift - UIBarButtonItem UIButton view alpha not workingswift - UIBarButtonItem UIButton 视图 alpha 不起作用
【发布时间】:2016-10-02 09:56:33
【问题描述】:

我正在使用以下代码在扩展程序中创建一个 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 工作。

【问题讨论】:

    标签: swift uibarbuttonitem


    【解决方案1】:

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

     override func viewWillAppear(_ animated: Bool) {
    
            button?.alpha = 0.4
        }
    

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

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多