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

How to change the icon of a Bar Button when pressed in Swift?

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

您无法在运行时更改 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

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

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

为Swift3

这就是我在 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")
}

为Swift4

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")
}