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

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

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

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

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

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) 

自定义“按下”UIBarButtonItem背景的想法