我有一个 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.

最佳答案

一种方法是使用 UIBarButtonItem 创建 customView 并使用 userInteractionEnabled 代替 enabled

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

关于ios - 禁用时如何使 UIBarButton 不灰显?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34599350/

10-10 04:09