在我的应用程序中,我想删除UINavigationBar
后退按钮标题。
我已经完成了以下代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// do other staffs
initNavigationBar()
return true
}
private func initNavigationBar() {
let appearance = UINavigationBar.appearance()
appearance.barTintColor = GLOBAL_TINT_COLOR // a globally declared colour
appearance.tintColor = .white
appearance.barStyle = .black
if #available(iOS 13.0, *) {
let backButtonAppearance = UIBarButtonItemAppearance()
backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
appearance.standardAppearance.backButtonAppearance = backButtonAppearance
appearance.compactAppearance?.backButtonAppearance = backButtonAppearance
appearance.scrollEdgeAppearance?.backButtonAppearance = backButtonAppearance
} else {
// Hide navigation bar back button items
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .highlighted)
}
}
但是,此代码始终适用于iOS 10-12
,但不适用于iOS 13
。我想念什么吗?
在其他情况下,我找到了有关该主题的许多答案,但没有找到iOS 13
我从不想使用set back button title as an empty string
,而不是使用外观对其进行修复。
谢谢
答案 0 :(得分:1)
几周前,我遇到了类似的问题。我没有找到在整个应用程序中全局执行此操作的方法,因此我求助于自定义每个导航控制器(值得庆幸的是,它并不多)。
我通过扩展UINavigationController
做过这样的事情:
@available(iOS 13, *)
func hideBackButton() {
let appearance = self.navigationBar.standardAppearance
let hideBackButtonTitleAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.clear
]
let normalBackButton = appearance.backButtonAppearance.normal
let highlightedBackButton = appearance.backButtonAppearance.highlighted
normalBackButton.titleTextAttributes = hideBackButtonTitleAttributes
highlightedBackButton.titleTextAttributes = hideBackButtonTitleAttributes
navigationBar.standardAppearance = appearance
}
然后我像这样使用hideBackButton
方法:
navigationController?.hideBackButton()
如果有更好的方法可以对整个应用程序执行此操作,请告诉我。
答案 1 :(得分:0)
您可以将您的后置商品设置为没有这样的标题:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
请注意,这会影响将其他内容推入导航堆栈时显示的内容。如果将视图控制器A设置为导航控制器的根,并这样设置A的后项,则在将视图控制器B推入堆栈时会看到它。设置B的后项不会影响您在B可见时在“后项”中看到的内容。