禁用时如何使UIBarButton不显示灰色?

时间:2016-01-04 20:32:52

标签: ios swift customization uibarbuttonitem

我有UIBarButton saveBarButton将屏幕图像保存到相机胶卷。当用户按下saveBarButton时,图像被保存,UIBarButton saveBarButton更改为UIImage doneIcon 1.2秒,以向用户指示图像已被保存。当saveBarButton显示doneIcon我禁用saveBarButton时。但是,当我这样做时,saveBarButton显示为灰色。

我的问题是:如何禁止UIBarButton在禁用时变灰?

我的代码:

//create UIBarButton saveBarButton
override func viewDidLoad() {    
    let saveBarButton = UIBarButtonItem(image: UIImage(named: "saveIcon"), style: .Plain, target: self, action: "save:")
    saveBarButton.tintColor = colorGreyDark
}

//save function called when press saveBarButton
func save(sender: UIBarButtonItem) {
    //save image
    deselectShape()
    let window: UIWindow! = UIApplication.sharedApplication().keyWindow
    let windowImage = capture(window)
    UIImageWriteToSavedPhotosAlbum(windowImage
        , nil, nil, nil)

    //Change saveBarButton to indicate to user that image was saved 
    sender.image = UIImage(named: "doneIcon")
    sender.enabled = false //disable saveBarButton
    self.performSelector("canSaveAgain:", withObject: sender, afterDelay: 1.2)
}

//Change saveBarButton to original icon to indicate to user that can save another image
func canSaveAgain(sender: UIBarButtonItem){
    sender.image = UIImage(named: "saveIcon")
    sender.enabled = true //enable saveBarButton
}

To see what I'm talking about.

1 个答案:

答案 0 :(得分:4)

其中一种方法是使用UIBarButtonItem创建customView,然后使用userInteractionEnabled代替enabled

let saveBarButton = UIBarButtonItem(customView: saveButton)
saveBarButton.customView?.userInteractionEnabled = false