swift - UIBarButtonItem UIButton View alpha 不起作用

标签 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 中的自定义 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/

相关文章:

html - 如何在 Swift 中匿名从 URL 获取 HTML 源代码?

ios - 条形按钮项目未正确对齐

ios - Swift: 将 UIView 设置为 UIBarButton/UIButton 的背景

ios - 具有自定义图像且无边框的 UIBarButtonItem

swift - SKAction.moveBy 不工​​作,我不知道为什么

ios - Swift 转换后的 Objective-C 方法签名相同

swift - 将图像编码为 base64 时的字符串无效 - Swift 4

ios - 项目在 Xcode 9.0.1 上运行正常,但在 Xcode 9.2 上显示错误

swift - 如何使用 Swift 将背景图像和标签添加到 UIBarButtonItem

ios - 如何减少应用程序导航栏中的“backButton”图像尺寸?