Make a UIBarButtonItem disappear using swift IOS

前端 未结 15 2211
慢半拍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: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.

提交回复
热议问题