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条回答
狗以群分
2楼-- · 2019-01-26 03:25
Try these:

 self.navigationController?.navigationBar.backItem?.title = ""
        navigationItem.backBarButtonItem?.title = ""
        navigationItem.leftBarButtonItem?.title = ""
navigationItem.hidesBackButton = true


        navigationItem.setLeftBarButtonItem(nil, animated: true)
        navigationItem.setRightBarButtonItem(nil, animated: true)
查看更多
地球回转人心会变
3楼-- · 2019-01-26 03:27

For Swift 3

if (Show_condition) {
  self.navigationItem.rightBarButtonItem = self.addAsset_btn
 }
else {
  self.navigationItem.rightBarButtonItem = nil
 }
查看更多
趁早两清
4楼-- · 2019-01-26 03:27

heres my solution:

hide:

self.creeLigueBouton.title = ""
self.creeLigueBouton.style = UIBarButtonItemStyle.Plain
self.creeLigueBouton.enabled = false

show:

self.creeLigueBouton.title = "Original Button Text"
self.creeLigueBouton.style = UIBarButtonItemStyle.Bordered
self.creeLigueBouton.enabled = true
查看更多
The star\"
5楼-- · 2019-01-26 03:29

The following solution works for me.

        var skipButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    skipButton.frame = CGRectMake(10.0, 0.0, 58.0, 32.0);
    skipButton.setTitle("Skip", forState: UIControlState.Normal)
    skipButton.setTitleColor(UIColor(red: 0.0, green: 122.0/255.0, blue: 255.0/255.0, alpha: 1.0), forState: UIControlState.Normal)
    skipButton.addTarget(self, action: "rightButtonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    var skipButtonItem = UIBarButtonItem(customView: skipButton)
    self.navigationItem.rightBarButtonItem = skipButtonItem;

    if hideSkipButton == true {
        self.navigationItem.rightBarButtonItem = nil
    } 
查看更多
我命由我不由天
6楼-- · 2019-01-26 03:30

You can use text attributes to hide a bar button:

barButton.enabled = false
barButton.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: .Normal)

Also I've made extension for UIBarButtonItem with a hidden property:

extension UIBarButtonItem {

    var titleTextAttributes: [NSObject : AnyObject]! {
        set {
            setTitleTextAttributes(newValue, forState: .Normal)
        }

        get {
            return titleTextAttributesForState(.Normal)
        }

    }

    private static var savedAttributesKey = "savedAttributes"

    var savedAttributes: [NSObject : AnyObject]? {
        set {
            objc_setAssociatedObject(self, &UIBarButtonItem.savedAttributesKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
        get {
            return objc_getAssociatedObject(self, &UIBarButtonItem.savedAttributesKey) as? [NSObject : AnyObject]
        }
    }

    var hidden: Bool {
        set {
            enabled = !newValue

            if newValue {
                savedAttributes = titleTextAttributes

                // Set a clear text color
                var attributes = titleTextAttributes
                attributes[NSForegroundColorAttributeName] = UIColor.clearColor()
                titleTextAttributes = attributes
            }
            else {
                titleTextAttributes = savedAttributes
            }
        }

        get {
            return enabled
        }
    }
}
查看更多
beautiful°
7楼-- · 2019-01-26 03:39

First way:

Just set .title to ""

Second way:

Just call updateToolBar() whenever you want to show/hide the creeLigueBouton.

func updateToolBar() {
    var barItems: [UIBarButtonItem] = []

    if condition != true {
        // Make it appear
        barItems.append(creeLigueBouton)
    }

    barItems.append(anotherButton)

    myToolBar.setItems(barItems, animated: true)

    myToolBar.setNeedsLayout()
}
查看更多
登录 后发表回答