如何防止我的UINavigationBar在SwiftUI中滚动时更改颜色?

时间:2019-11-03 15:34:54

标签: ios swift swiftui

我有一个视图,可将其推到将导航栏设置为透明的位置,并且出于布局原因,我将其中一个子视图设置为-180 pts。滚动时,我不希望导航栏更改为半透明的黑色,而是希望它保持不可见。我该怎么做?

 var body: some View {
        ScrollView {
            VStack(alignment: .center) {
               // some views, not important
            }
            .background(SwiftUI.Color.white)
            .foregroundColor(.black)
            .padding(.top, -180)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .navigationBarItems(
            leading: self.centeredButton(systemName: "arrow.left", action:dismiss),
            trailing: self.centeredButton(systemName: "square.and.arrow.up", action: { })
        )
}

private func transparentNavBar() -> NavigationConfigurator {
        return NavigationConfigurator { nc in
            nc.navigationBar.barTintColor = .white
            nc.navigationBar.setBackgroundImage(UIImage(), for: .default)
            nc.navigationBar.shadowImage = UIImage()
            nc.navigationBar.isTranslucent = false
        }
}

//https://stackoverflow.com/questions/56505528/swiftui-update-navigation-bar-title-colors
struct NavigationConfigurator: UIViewControllerRepresentable {

    var configure: (UINavigationController) -> Void = { _ in }

    func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController {
        UIViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
        if let nc = uiViewController.navigationController {
            self.configure(nc)
        }
    }

}

0 个答案:

没有答案