SwiftUI –导航栏“ isTranscluent”属性在iPad上崩溃

时间:2020-06-27 06:38:47

标签: swift ipad swiftui uinavigationbar swiftui-navigationlink

如何使用SwiftUI为iPad导航栏设置isTranscluent属性。下面的代码在iPhone中可以正常运行,但在iPad中会崩溃,并显示错误:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

UINavigationBar.appearance().isTranslucent = false

1 个答案:

答案 0 :(得分:0)

使用UIKit在Xcode 12.0 Beta(iPad Pro 2018和iPadOS 13.5)中工作正常:

navigationController.navigationBar.isTranslucent = false


使用SwiftUI在Xcode 12.0 Beta(iPad Pro 2018和iPadOS 13.5)中也可以正常工作:

struct NavigationConfigurator: UIViewControllerRepresentable { ... }

NavigationView {

    ScrollView {
        Text("Scroll View")
    }
    .navigationBarTitle("Navigation Bar", displayMode: .inline)
    .background(NavigationConfigurator { controller in
        controller.navigationBar.barTintColor = .red
        controller.navigationBar.titleTextAttributes = [.foregroundColor : .grey]
        controller.navigationBar.isTranslucent = false
    })
}