代码之家  ›  专栏  ›  技术社区  ›  Casey Perkins

bg UIViewControllerRepresentable中视图控制器状态栏区域的颜色

  •  0
  • Casey Perkins  · 技术社区  · 4 年前

    我使用UIViewControllerRepresentable作为视图控制器,它可以完成我在SwiftUI中无法完成的事情。我试图让状态栏区域显示与深蓝色相同的颜色,但它仍然是白色。我早些时候尝试将它嵌入导航控制器中,但也没有成功。(在左边的层次结构中忽略它下面的表和视图,当我试图弄清楚顶部的颜色发生了什么时,我将它们隐藏起来)。有什么想法可以强迫顶部区域符合所需的颜色吗?

    非常感谢。

    enter image description here

    编辑后添加: UIViewControllerRepresentable中的代码:

        func makeUIViewController(context: Context) -> UINavigationController {
            let storyboard = UIStoryboard(name: "RoundEntry", bundle: nil)
            
            let roundEntryVC = storyboard.instantiateViewController(identifier:
                    "RoundEntry") as! RoundEntryVC
            let navigationController  = UINavigationController(rootViewController: roundEntryVC)
            return navigationController
        }
    

    以下是我在RoundEntry VC中更改导航条颜色的尝试(在viewDidLoad中):

            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.backgroundColor = UIColor(.orange)
            navigationController?.navigationBar.standardAppearance = navBarAppearance
            navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
            navigationController?.navigationBar.tintColor = .white
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   tsr    4 年前

    你可能正在寻找

    .edgesIgnoringSafeArea(.top)
    

    其中您的UIViewControllerRepresentable在SwiftUI中使用。

    它在我的案例中起作用,我还嵌入了容器视图控制器。可能会有一些我不知道的副作用,所以请使用它。