在Swift中按下时如何更改条形按钮的图标? [英] How to change the icon of a Bar Button when pressed in Swift?

查看:110
本文介绍了在Swift中按下时如何更改条形按钮的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a stopwatch in Swift and I want to change the play icon I have selected for a bar button to a pause icon when the button is pressed to start the stopwatch. How do you do this?

推荐答案

对于Swift 3

这就是我在Swift 3中的做法:

This is how I did it in 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 favoritesBarButt onOn:UIBarButtonItem!
var favoritesBarButtonOFF:UIBarButtonItem!

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

这篇关于在Swift中按下时如何更改条形按钮的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆