Make a UIBarButtonItem disappear using swift IOS

前端 未结 15 2156
慢半拍i
慢半拍i 2020-12-10 23:31

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

@IBOutlet var creeLigueBouton: UIBarButtonItem!

and I want to make it disappea

相关标签:
15条回答
  • 2020-12-11 00:19

    If you have set of UIBarButtonItems to hide, e.g. only show them on Landscape orientation, and hide or Portrait, you can use tag and Swift Array's filter. Let's assume we made @IBOutlet link to UIToolBar:

    @IBOutlet weak var toolbar: UIToolbar!
    

    First, we save toolbar's items in viewDidLoad:

    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
      toolbarItems = toolbar.items
    }
    

    Set the tag property of UIBarButtonItem you want to show on Landscape orientation to 1 or whatever you like. Then, override func traitCollectionDidChange

    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        switch (traitCollection.horizontalSizeClass, traitCollection.verticalSizeClass) {
        case (.Compact, .Regular): // iPhone Portrait
          let items: [UIBarButtonItem]?
          if view.frame.width > 320 { // iPhone 6 & 6S
            items = toolbarItems?.filter({ $0.tag < 5 })
          } else { 
            items = toolbarItems?.filter({ $0.tag < 4 })
          }
          bottomToolbar.setItems(items, animated: true)
        case (_, .Compact): // iPhone Landscape
          let items = toolbarItems?.filter({ $0.tag < 6 })
          bottomToolbar.setItems(items, animated: true)
        default: // iPad
          break
        }
      }
    

    In this example, I set all UIBarButtonItem's tag for iPad only to 6, iPhone Landscape to 5, and for iPhone 6 & 6+ to 4.

    0 讨论(0)
  • 2020-12-11 00:23

    I had the same problem with a tool bar that I had to hide and show its last button. So I declared a var to hold the UIBarButtonItem and removed it from the bar or added depending on the situation like:

    inside the class declared the var and linked the toolbar:

    var buttonToHide : UIBarButtonItem?
    
    @IBOutlet weak var toolbarOne: UIToolbar!
    

    at the viewDidLoad :

    buttonToHide = toolbarOne.items![toolbarOne.items!.count - 1] as? UIBarButtonItem
    

    in my code I made the trick:

    if situationOccurrsToHide {
       toolbarOne.items!.removeLast()
    }
    

    or

    if situationOccursToShow 
    {    
       toolbarOne.items!.append(buttonToHide!) 
    }
    

    You can use the removeAtIndex or insert(buttonToHide, atIndex: xx) to remove or reinsert the button at a specific position.

    You must be careful not to insert or remove the button more than once.

    Hope it helps.

    0 讨论(0)
  • 2020-12-11 00:24

    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
    
    0 讨论(0)
  • 2020-12-11 00:25

    For Swift 3

    if (your_condition) {
      self.navigationItem.rightBarButtonItem = self.addAsset_btn
     }
    else {
      // hide your button
      self.navigationItem.rightBarButtonItem = nil
     }
    
    0 讨论(0)
  • 2020-12-11 00:25

    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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 00:30

    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()
    }
    
    0 讨论(0)
提交回复
热议问题