【问题标题】:How to make UIBarButton not greyed out when disabled?禁用时如何使 UIBarButton 不灰显?
【发布时间】:2016-01-04 20:32:52
【问题描述】:

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

【问题讨论】:

  • 你试过sender.tintColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1.0),把按钮设置为启用=false?

标签: ios swift customization uibarbuttonitem


【解决方案1】:

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

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

【讨论】:

  • 有趣的想法...我实际上已经更改了我的代码,所以我根本不需要这样做,但感谢您的帮助!我会在需要时记住这一点
猜你喜欢
  • 1970-01-01
  • 2011-01-15
  • 2018-05-20
  • 2015-05-04
  • 2012-02-01
  • 1970-01-01
  • 2018-09-07
  • 2019-07-27
  • 1970-01-01
相关资源
最近更新 更多