如何在Swift中按下时更改条形按钮的图标?

时间:2015-06-10 23:23:28

标签: ios swift uibarbuttonitem

我正在Swift中创建一个秒表,当按下按钮启动秒表时,我想将我为条形按钮选择的播放图标更改为暂停图标。你是怎么做到的?

3 个答案:

答案 0 :(得分:4)

您无法在运行时更改UIBarButtonItem的样式。您必须删除UIBarButtonItem,然后添加您想要的UIBarButtonItem

@IBOutlet weak var toolBar: UIToolbar!
var pauseButton = UIBarButtonItem()
var playButton = UIBarButtonItem()
var arrayOfButtons = [AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()
    pauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButtonTapped")
    playButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButtonTapped")

    arrayOfButtons = self.toolBar.items!
    arrayOfButtons.insert(playButton, atIndex: 0) // change index to wherever you'd like the button
    self.toolBar.setItems(arrayOfButtons, animated: false)
}

func playButtonTapped() {
    arrayOfButtons = self.toolBar.items!
    arrayOfButtons.removeAtIndex(0) // change index to correspond to where your button is
    arrayOfButtons.insert(pauseButton, atIndex: 0)
    self.toolBar.setItems(arrayOfButtons, animated: false)
}

func pauseButtonTapped() {
    arrayOfButtons = self.toolBar.items!
    arrayOfButtons.removeAtIndex(0) // change index to correspond to where your button is
    arrayOfButtons.insert(playButton, atIndex: 0)
    self.toolBar.setItems(arrayOfButtons, animated: false)
}

UIBarButtonItem Class Reference

答案 1 :(得分:1)

适用于Swift 3

这就是我在Swift 3中的表现:

var favoritesBarButtonOn: UIBarButtonItem!
var favoritesBarButtonOFF: UIBarButtonItem!

favoritesBarButtonOn = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOff"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOn))
favoritesBarButtonOFF = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOn"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOFF))

self.navigationItem.rightBarButtonItems = [self.rightNavBarButton, self.favoritesBarButtonOn]

func didTapFavoritesBarButtonOn() {
    self.navigationItem.setRightBarButtonItems([self.rightNavBarButton, self.favoritesBarButtonOFF], animated: false)
    print("Show Favorites")
}

func didTapFavoritesBarButtonOFF() {
    self.navigationItem.setRightBarButtonItems([self.rightNavBarButton, self.favoritesBarButtonOn], animated: false)
    print("Show All Chat Rooms")
}

适用于Swift 4

var favoritesBarButtonOn:UIBarButtonItem!     var favoritesBarButtonOFF:UIBarButtonItem!

favoritesBarButtonOn = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOff"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOn))
favoritesBarButtonOFF = UIBarButtonItem(image: #imageLiteral(resourceName: "TabBarIconSettingsOn"), style: .plain, target: self, action: #selector(didTapFavoritesBarButtonOFF))

self.navigationItem.rightBarButtonItems = [self.favoritesBarButtonOn]

func didTapFavoritesBarButtonOn() {
    self.navigationItem.setRightBarButtonItems([self.favoritesBarButtonOFF], animated: false)
    print("Show Favorites")
}

func didTapFavoritesBarButtonOFF() {
    self.navigationItem.setRightBarButtonItems([self.favoritesBarButtonOn], animated: false)
    print("Show All Chat Rooms")
}

答案 2 :(得分:0)

我相信你已经为你的问题找到了解决方案,但我会留下这个以防任何人仍然需要它。

UIBarButtonItem不是UIControl,但是您可以使用自定义视图对其进行初始化,即以编程方式自定义UIButton,如下所示:

let playButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
playButton.addTarget(self, action: "togglePlay:", forControlEvents: .TouchUpInside)
playButton.setImage(UIImage(named: "play-active"), forState: .Normal)
playButton.setImage(UIImage(named: "play-inactive"), forState: .Selected)
let rightButton = UIBarButtonItem(customView: playButton)
self.navigationItem.setRightBarButtonItems([rightButton], animated: true)

来自Custom "Pressed" UIBarButtonItem Backgrounds

的想法