In my application, I want to remove the UINavigationBar
Back Button title. I have done the following codes
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)
}
}
However, this code always works for iOS 10-12
, but not working for iOS 13
. Am I missing something?
In other cases, I have found many answers regarding the topic, but found no solution for iOS 13
I never want to use to set back button title as an empty string
, rather than fixing it using appearance.
Thanks
I had a similar issue some weeks ago. I did not find a way to do it globally for the entire application, so I resorted to customizing each navigation controller (which thankfully wasn't many).
I did something like this by extending 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
}
Then I used the hideBackButton
method like so:
navigationController?.hideBackButton()
If there is a better way to do this for the entire application, let me know.
You can set your back item to have no title like this:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
Note that this affects what shows up when something else is pushed onto the navigation stack. If you set view controller A as the root of the navigation controller and set A's back item like that, you'll see it when you push view controller B onto the stack. Setting B's back item will not affect what you see in the Back item when B is visible.
I have added ios15 remove BackBar button title and Set Navigations:
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
let pargraphStyle = NSMutableParagraphStyle()
pargraphStyle.alignment = .center
let fontApply = UIFont.bold(size: 18)
UINavigationBar.appearance().tintColor = UIColor.themeColor
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.black
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.themeColor, NSAttributedString.Key.font: fontApply, NSAttributedString.Key.paragraphStyle: pargraphStyle]
appearance.shadowImage = nil
appearance.shadowColor = .clear
UINavigationBar.appearance().barStyle = .default
UINavigationBar.appearance().tintColor = UIColor.themeColor
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "BackIcon")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "BackIcon")
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
// Set Back Bar Button Appearance
let appearanceBackButton = UIBarButtonItemAppearance()
let hideBackButtonTitleAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.clear
]
let normalBackButton = appearance.backButtonAppearance.normal
let highlightedBackButton = appearance.backButtonAppearance.highlighted
normalBackButton.titleTextAttributes = hideBackButtonTitleAttributes
highlightedBackButton.titleTextAttributes = hideBackButtonTitleAttributes
appearance.buttonAppearance = appearanceBackButton
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.