How to use UIBarButtonSystemItem to change UIBarButtonItem identifier? (Swift)

I want to change the UIBarButtonItem identifier with codes from "Play" to "Pause". How can i do this?

thanks

+8
source share
5 answers

1) launch a new button

//change to play let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "someAction") navigationBar.topItem.leftBarButtonItem = button //change to pause let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "someOtherAction") navigationBar.topItem.leftBarButtonItem = button 

2) Just change the text:

 navigationBar.topItem?.leftBarButtonItem?.title = "AnyText" 

If you also have problems accessing the navigation bar, it's probably best to just set a tag for it (I like to use negative tags for certain views * to make sure that 2 views * don't get the same tag) Then you can do it like this:

 let navigationBar = (self.view.viewWithTag(-1) as UINavigationBar) navigationBar.topItem?.leftBarButtonItem?.title = "AnyText" 
+17
source

I use this code to switch edit mode

 @IBAction func editMode(sender: UIBarButtonItem) { self.setEditing(!self.isEditing, animated: true) let newButton = UIBarButtonItem(barButtonSystemItem: (self.isEditing) ? .done : .edit, target: self, action: #selector(editMode(sender:))) self.navigationItem.setLeftBarButton(newButton, animated: true) } 
+8
source

For a toolbar item, here is a sample code.

 var player:AVAudioPlayer = AVAudioPlayer() @IBOutlet var toolbar: UIToolbar! @IBOutlet var playPauseButton: UIBarButtonItem! @IBAction func playPause(sender: AnyObject) { if player.playing { player.pause() playPauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:") toolbar.items![0] = playPauseButton //apply for first toolbar item } else { player.play() playPauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause:") toolbar.items![0] = playPauseButton //apply for first toolbar item } } 
+4
source

This code has been tested and works with Swift 2

 @IBOutlet weak var navigationBar: UINavigationBar! //playToPause() @IBAction func playButton(sender: UIBarButtonItem) { let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:") navigationBar.topItem?.rightBarButtonItem = newBarButton } // pauseToPlay() @IBAction func pauseButton(sender: UIBarButtonItem){ let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:") navigationBar.topItem!.rightBarButtonItem = pauseBtnItem } 
+1
source

I have seen several threads and this is the only answer that actually does what I am looking for. The only problem I am facing is that it moves the toolbar item to the leftmost position, and I'm focused on it. If you have a button centered with a flexible space bar or if it is not the first, just change the index as follows: toolbar.items![1] = playPauseButton //apply for second toolbar item

0
source

Source: https://habr.com/ru/post/981705/


All Articles