【问题标题】:UINavigationBarAppearance update after view controller has appeared视图控制器出现后 UINavigationBarAppearance 更新
【发布时间】:2019-10-10 08:51:04
【问题描述】:

在使用新的UINavigationBarAppearanceapis 时,似乎没有(或至少没有明显的)方法来更新视图控制器上的导航样式。虽然这些 API 远优于旧的样式方法,尤其是在需要透明导航栏时,但我需要在多个应用中能够在透明导航和不透明导航之间切换。

有人知道解决方法吗?或者我可能会错过什么?示例代码如下!

var styles: [UINavigationBarAppearance] = {

    let style1 = UINavigationBarAppearance()
    style1.configureWithOpaqueBackground()
    style1.backgroundColor = .white
    style1.titleTextAttributes = [.foregroundColor: UIColor.black]
    style1.largeTitleTextAttributes = [.foregroundColor: UIColor.black]

    let buttonAppearance = UIBarButtonItemAppearance()
    buttonAppearance.normal.titleTextAttributes = [
        .foregroundColor: UIColor.black
    ]
    style1.buttonAppearance = buttonAppearance

    let style2 = UINavigationBarAppearance()
    style2.configureWithOpaqueBackground()
    style2.backgroundColor = .systemBlue
    style2.titleTextAttributes = [.foregroundColor: UIColor.systemGreen]
    style2.largeTitleTextAttributes = [.foregroundColor: UIColor.systemGreen]

    let button2Appearance = UIBarButtonItemAppearance()
    button2Appearance.normal.titleTextAttributes = [
        .foregroundColor: UIColor.systemGreen
    ]
    style2.buttonAppearance = button2Appearance

    let style3 = UINavigationBarAppearance()
    style3.configureWithTransparentBackground()
    style3.titleTextAttributes = [.foregroundColor: UIColor.systemBlue]
    style3.largeTitleTextAttributes = [.foregroundColor: UIColor.systemBlue]

    let button3Appearance = UIBarButtonItemAppearance()
    button3Appearance.normal.titleTextAttributes = [
        .foregroundColor: UIColor.systemBlue
    ]
    style3.buttonAppearance = button3Appearance

    return [style1, style2, style3]

}()

var currentStyle: UINavigationBarAppearance?

override func viewDidLoad() {

    currentStyle = styles.first
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

@IBAction func switchNavigavigationStyles(_ sender: Any) {

    guard let currentStyle = currentStyle else {
        return
    }
    guard let styleIndex = styles.firstIndex(of: currentStyle) else {
        return
    }

    var nextIndex = styleIndex + 1
    if nextIndex == styles.count {
        nextIndex = 0
    }

    navigationController?.navigationBar.scrollEdgeAppearance = styles[nextIndex]
    navigationController?.navigationBar.compactAppearance = styles[nextIndex]
    navigationController?.navigationBar.standardAppearance = styles[nextIndex]
    navigationController?.navigationBar.setNeedsDisplay() // Attempting to force it to redraw!
    navigationController?.view.setNeedsDisplay()

    self.currentStyle = styles[nextIndex]
}

【问题讨论】:

    标签: ios uinavigationcontroller uikit uinavigationbar ios13


    【解决方案1】:

    知道了!因此,在重新观看 WWDC 视频后,它似乎在您设置的特定屏幕上配置 navigationItem.standardAppearance e.t.c。已尝试改用它,它似乎可以完成这项工作!

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多