Swift 4环境下导航控制器返回按钮字体修改失效求助
Hey there! Let's break down your problem with customizing the back button font in your setup (Swift 4, iOS 11.2.5, Xcode 9.2, navigation controller nested inside a tab bar controller).
First off, since you mentioned only the first and last lines of your viewDidLoad code work but the middle three targeting the back button don't, it's almost certainly a timing or hierarchy-related issue—those middle lines are trying to modify a component that hasn't fully initialized yet, or they're targeting the wrong part of the navigation stack.
In iOS 11+, the back button in a navigation bar is actually managed by the previous view controller's navigation item, not the current one. Add a tab bar controller wrapping your navigation controllers into the mix, and there's an extra layer of delay in when navigation bar components get set up. Running your customization code immediately in viewDidLoad means you're likely trying to access a back button that doesn't exist yet.
Here are a few proven approaches that should work for your setup:
Option 1: Use the Appearance Proxy (Global or Targeted)
This method applies styles early enough in the view hierarchy to avoid timing issues. You can set this in your app delegate'sdidFinishLaunchingWithOptionsor the navigation controller'sviewDidLoad:let backButtonStyle: [NSAttributedString.Key: Any] = [ .font: UIFont(name: "YourCustomFont", size: 16)! ] // Apply globally to all back buttons UIBarButtonItem.appearance().setTitleTextAttributes(backButtonStyle, for: .normal) UIBarButtonItem.appearance().setTitleTextAttributes(backButtonStyle, for: .highlighted) // Or target only your specific navigation controller // UIBarButtonItem.appearance(whenContainedInInstancesOf: [YourNavigationController.self]).setTitleTextAttributes(backButtonStyle, for: .normal)Option 2: Customize the Previous View Controller's Back Item
Since the back button is tied to the view controller that pushed the current one, define a custom back button in the previous VC'sviewDidLoad:let customBackButton = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil) customBackButton.setTitleTextAttributes([ .font: UIFont(name: "YourCustomFont", size: 16)! ], for: .normal) navigationItem.backBarButtonItem = customBackButtonThis ensures the back button uses your font as soon as the current VC is pushed onto the stack.
Option 3: Delay Customization in the Current VC
If you want to keep the code in your current VC'sviewDidLoad, wrap the back button customization in a main queue async call to give the navigation stack time to initialize:override func viewDidLoad() { super.viewDidLoad() // Your working first line here DispatchQueue.main.async { let backButtonStyle: [NSAttributedString.Key: Any] = [ .font: UIFont(name: "YourCustomFont", size: 16)! ] self.navigationController?.navigationBar.backItem?.backBarButtonItem?.setTitleTextAttributes(backButtonStyle, for: .normal) } // Your working last line here }
Any of these options should resolve the issue with your middle lines not executing effectively. The core problem was just that your original code ran too early, before the back button was ready to be modified.
内容的提问来源于stack exchange,提问作者user3069232

