0
votes

I'm creating a uibutton in an extension with the following code:

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
    }
}

The problem is that I'm using this button as a custom view in a UIBarButtonItem:

UIBarButtonItem(customView: self.customButton!)

That button is settingt he alpha to 0.4 but the button still shows up with full alpha!

I even tried switching it in the class after calling the customButton method and it simply does not work.

I'm trying to make the button seem disabled on first open and it's impossible to get that alpha to work.

1

1 Answers

0
votes

Not sure why this is happening but I added this and we're good now.

 override func viewWillAppear(_ animated: Bool) {

        button?.alpha = 0.4
    }

For some reason putting it in the viewWillAppear makes it so that the alpha is 0.4 on load.