You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Why Your Original Middle Lines Might Fail

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.

Reliable Fixes to Customize the Back Button Font

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's didFinishLaunchingWithOptions or the navigation controller's viewDidLoad:

    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's viewDidLoad:

    let customBackButton = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
    customBackButton.setTitleTextAttributes([
        .font: UIFont(name: "YourCustomFont", size: 16)!
    ], for: .normal)
    navigationItem.backBarButtonItem = customBackButton
    

    This 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's viewDidLoad, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:18:51