如何在运行时“干净地"更改 UIBarButtonItem 文本(编辑 --> 完成 --> 编辑)? [英] How to change UIBarButtonItem text during runtime "cleanly" (Edit --> Done --> Edit)?

查看:29
本文介绍了如何在运行时“干净地"更改 UIBarButtonItem 文本(编辑 --> 完成 --> 编辑)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时干净地"更改 UIBarButtonItem 文本,以便可以切换编辑"/完成"模式.然而,每次我在运行时更改标题属性时,动画似乎都很笨拙.我希望在通讯录"应用中模拟编辑/完成"按钮的外观和功能(其中编辑"只是淡出而完成"出现在其位置).

I'm trying to change UIBarButtonItem text "cleanly" during runtime so that Edit/Done modes can be toggled. Every time I change the title attribute during runtime, however, the animation seems clumsy. I'm looking to emulate the appearance and function of the Edit/Done button in the Contacts app (where Edit simply fades and Done appears in its place).

@IBAction func editDoneButtonPressed(sender: UIBarButtonItem) {

    if(sender.title == "Edit"){
        sender.title = "Done"
    }else{
        sender.title = "Edit"
    }

}

初始设置:标识符设置为自定义",标题设置为编辑"

Initial settings: Identifier is set to "custom" and Title is set to "Edit"

更简单的编程解决方案是首选,但是,动画的外观确实是最重要的.我会考虑切换 UIBarButtonItem 标识符,而不是它的文本属性,但是,我不确定在运行时切换标识符的优雅方式.

Simpler programatical solutions are preferred, however, the appearance of the animation is indeed paramount. I'd consider toggling the UIBarButtonItem identifier, rather than its text attribute, however, I'm not sure of an elegant way to toggle the identifier during runtime.

顺便说一句:我正在使用 Swift 来构建应用程序.

BTW: I'm using Swift to construct the app.

链接到...

通讯录应用编辑/完成切换动画的截屏:https://youtu.be/5mT_vzpNhIw

Screencast of Contacts app Edit/Done toggle animation: https://youtu.be/5mT_vzpNhIw

我的编辑/完成切换动画的截屏:https://youtu.be/mEweotJNVNE

Screencast of my Edit/Done toggle animation: https://youtu.be/mEweotJNVNE

谢谢.

推荐答案

有一种更好的方法,一种标准方法,可以获取编辑/完成"按钮.

There's a much better way, a standard way, to get an Edit/Done button.

UIViewController 提供了一个标准的 Edit/Done 按钮,该按钮已经连接到视图控制器的 editing 属性中.

UIViewController provides a standard Edit/Done button that is already hooked into the editing property of the view controller.

一个常见的用法如下:

在视图控制器的 viewDidLoad 方法中,使用标准按钮:

Inside the viewDidLoad method of your view controller, use the standard button:

self.navigationItem.rightBarButtonItem = editButtonItem

editButtonItem 放在您真正需要的地方.

Put the editButtonItem wherever you actually need it.

然后,无需设置您自己的操作,只需覆盖 setEditing(_:animated:) 方法:

Then, instead of setting up your own action, simply override the setEditing(_:animated:) method:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if (editing) {
        // user just tapped the Edit button (it now says Done)
    } else {
        // user just tapped the Done button (it now says Edit)
    }
}

这篇关于如何在运行时“干净地"更改 UIBarButtonItem 文本(编辑 --> 完成 --> 编辑)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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