How to use UIBarButtonSystemItem to change UIBarBu

2019-02-13 03:12发布

I want to change the identifier of an UIBarButtonItem with codes from "Play" to "Pause". How can I do that?

Thanks

5条回答
手持菜刀,她持情操
2楼-- · 2019-02-13 03:33

I use this code to switch Edit mode

@IBAction func editMode(sender: AnyObject) {
    self.setEditing(!self.editing, animated: true)
    let newButton = UIBarButtonItem(barButtonSystemItem: (self.editing) ? .Done : .Edit, target: self, action: #selector(editMode(_:)))
    self.navigationItem.setLeftBarButtonItem(newButton, animated: true)
}
查看更多
劫难
3楼-- · 2019-02-13 03:33

For a toolbar item, here is the 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楼-- · 2019-02-13 03:36

I saw looked at several threads and this is the only answer that actually does what I'm looking for. The only problem I have is that it moves my toolbar item to the far left, and I have it centered. If you have your button centered with a flexible space bar, or if it is not the first button, just change the index like this: toolbar.items![1] = playPauseButton //apply for second toolbar item

查看更多
时光不老,我们不散
5楼-- · 2019-02-13 03:53

1) init 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're also having trouble accessing the navigation bar it is probably best to just set some tag to it (I like to use negative tags for specific Views* to make sure 2 views* don't get the same tag). Then you could fx do like this:

let navigationBar = (self.view.viewWithTag(-1) as UINavigationBar)
navigationBar.topItem?.leftBarButtonItem?.title = "AnyText"
查看更多
\"骚年 ilove
6楼-- · 2019-02-13 03:54

This code is tested and working 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
}
查看更多
登录 后发表回答