【问题标题】:How to set the navigation bar's back bar button without using UIBarButtonItem.appearance如何在不使用 UIBarButtonItem.appearance 的情况下设置导航栏的后退栏按钮
【发布时间】:2022-09-23 11:10:50
【问题描述】:

我有一个像这样的自定义 UINavigationController:

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        delegate = self
        setupDefaultAppearance()
    }
    
    private func setupDefaultAppearance() {
        UINavigationBar.appearance().tintColor = R.color.textBlack()
        
        let titleAttributes: [NSAttributedString.Key: Any] = [ .font: R.font.interMedium(size: 18)! ]
        UINavigationBar.appearance().titleTextAttributes = titleAttributes
        
        // Hide the title in bar button items
        let backButtonAttributes: [NSAttributedString.Key: Any] = [ .font: UIFont(name: \"Helvetica-Bold\", size: 0.1)!,
                                                                    .foregroundColor: UIColor.clear]

        UIBarButtonItem.appearance().setTitleTextAttributes(backButtonAttributes, for: .normal)
        UIBarButtonItem.appearance().setTitleTextAttributes(backButtonAttributes, for: .highlighted)
    }
}

但是,隐藏栏按钮项目中的标题的代码搞砸了 IQKeyboardManager。因此,键盘或任何选择器视图中的 Done 按钮(或工具栏上的任何按钮)现在都消失了。所以,我相信我不应该使用 UIBarButtonItem.appearance() 静态函数。如何删除导航控制器的后退按钮的标题而不在键盘和选择器视图中产生错误?

谢谢。

    标签: ios swift uinavigationcontroller uikit


    【解决方案1】:

    所以我也向 Apple Developer Forums 提出了同样的问题。确实有人回答了。基本上,我需要做的只是外观是它包含在导航栏中,如下所示:

        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(backButtonAttributes, for: .normal)
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes(backButtonAttributes, for: .highlighted)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      相关资源
      最近更新 更多