繁体   English   中英

如何在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类参考

对于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不是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背景的想法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM