Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视图控制器出现后的UINavigationBarAppearance更新_Ios_Uinavigationcontroller_Uikit_Uinavigationbar_Ios13 - Fatal编程技术网

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

Ios 视图控制器出现后的UINavigationBarAppearance更新,ios,uinavigationcontroller,uikit,uinavigationbar,ios13,Ios,Uinavigationcontroller,Uikit,Uinavigationbar,Ios13,在使用新的UINavigationBarAppearance时,似乎没有(或者至少没有明显的)方法来更新视图控制器上的导航样式。虽然这些API远远优于旧的样式设置方法,特别是当需要一个透明的导航栏时,我需要在多个应用程序中能够在透明导航和不透明导航之间切换 有人知道这方面的解决方法吗?或者我可能错过了什么?下面的示例代码 var styles: [UINavigationBarAppearance] = { let style1 = UINavigationBarAppearance(

在使用新的
UINavigationBarAppearance
时,似乎没有(或者至少没有明显的)方法来更新视图控制器上的导航样式。虽然这些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]
}

明白了!因此,在重新观看WWDC视频后,它似乎要在您设置的特定屏幕上配置
navigationItem。standardAppearance
e.t.c.已尝试使用此选项,它似乎可以完成此任务