2016-10-02 61 views
0

我創建與下面的代碼擴展一個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工作。

回答

0

不知道爲什麼會發生這種情況,但我補充說,現在我們很好。

override func viewWillAppear(_ animated: Bool) { 

     button?.alpha = 0.4 
    } 

由於某種原因,將它放在viewWillAppear中使得它的alpha值爲0.4。