Make a UIBarButtonItem disappear using swift IOS

2019-01-26 03:15发布

I have an IBOutlet that I have linked to from the storyboard

@IBOutlet var creeLigueBouton: UIBarButtonItem!

and I want to make it disappear if a condition is true

if(condition == true)
{
    // Make it disappear
}

15条回答
Deceive 欺骗
2楼-- · 2019-01-26 03:50

I did it using this:

navigationItem.setHidesBackButton(true, animated: true)
查看更多
狗以群分
3楼-- · 2019-01-26 03:51

Use the property enabled and tintColor

    let barButtonItem:UIBarButtonItem? = nil

    if isHidden{
        barButtonItem?.enabled      = false
        barButtonItem?.tintColor    = UIColor.clearColor()
    }else{
        barButtonItem?.enabled      = true
        barButtonItem?.tintColor    = nil
    }
查看更多
Fickle 薄情
4楼-- · 2019-01-26 03:51
// Nice answer haiLong, I think as an extension this is more convenient.

extension UIBarButtonItem {
    var isHidden: Bool {
        get {
            return !isEnabled && tintColor == .clear
        }
        set {
            tintColor = newValue ? .clear : nil
            isEnabled = !newValue
        }
    }
}

EDIT: Removed forced unwrapping and fixed enabled value.

查看更多
登录 后发表回答