首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视图控制器出现后的UINavigationBarAppearance更新

视图控制器出现后的UINavigationBarAppearance更新
EN

Stack Overflow用户
提问于 2019-10-10 16:51:05
回答 1查看 108关注 0票数 0

在使用新的UINavigationBarAppearance apis时,似乎没有--或者至少没有明显的--方法来更新视图控制器上的导航样式。虽然这些API远远优于旧的样式方法,特别是当我想要一个透明的导航栏时,我需要多个应用程序才能在透明导航和不透明导航之间切换。

有没有人知道解决这个问题的办法?或者我可能遗漏了什么?下面是示例代码!

代码语言:javascript
复制
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]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-10 18:03:50

明白了!因此,在重新观看WWDC视频之后,似乎需要在特定屏幕上设置navigationItem.standardAppearance e.t.c。我尝试使用它来代替,并且它似乎已经完成了工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58318955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档