为所有UIViewController实例设置全局默认后退按钮显示模式 [英] Set global default back button display mode for all UIViewController instances
问题描述
iOS 14引入了可配置的后退按钮模式.例如.您可以将返回"按钮上的标签文本,但是在紧凑的历史记录菜单中,您仍然可以看到以前控制器的正确标题.
iOS 14 introduced configurable Back button mode. Eg. you can have "Back" label text on the button, but in the compact history menu you can still see the proper title of previous controllers.
我正在寻找一种配置默认模式的简便方法,因此在运行时,所有UIViewController实例都将设置默认模式,例如 UINavigationItemBackButtonDisplayModeGeneric
I'm looking for a easy, pleasant way to configure the default mode, so all UIViewController instances during runtime will have default mode set, like UINavigationItemBackButtonDisplayModeGeneric
我想知道是否有一种方法可以不对UIViewController进行子类化,或者记得始终手动配置每个UIViewController实例(通过 viewController.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeGeneric
).
I wonder if there is a way to do that without subclassing UIViewController or remember to always configure manually every instance of UIViewController
(via viewController.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeGeneric
).
任何不需要大量重构数百个UIViewController实例的便捷方法,将不胜感激!
Any handy method which does not require extensive refactoring of hundreds of UIViewController instances greatly appreciated!
推荐答案
没有子类,我认为这是不可能的,因为 navigationItem
需要使用一个实例,并且不能直接从扩展中进行修改>
Without subclassing I think is not possible since navigationItem
requires an instance to work with and can't be modified directly from extensions
class GenericViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// your code here
viewController.navigationItem.backButtonDisplayMode = UINavigationItemBackButtonDisplayModeGeneric
}
}
并在需要的地方使用它
class viewController: GenericViewController
这是一个非常好的方法,因为您可以控制实现它的方式以及不考虑可能并非在所有场景中都存在的方式
This is a really good approach since you have control over what implements it and what's not considering that it might not be there in all scenes
这篇关于为所有UIViewController实例设置全局默认后退按钮显示模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!