【问题标题】:SwiftUI - How to change the background Color of top safe area to gray?SwiftUI - 如何将顶部安全区域的背景颜色更改为灰色?
【发布时间】:2021-03-06 12:01:27
【问题描述】:

我想将顶部安全区域的背景颜色从绿色更改为灰色。我到处寻找,但找不到任何解决方案。预览画面是这样的。

我的代码:

struct ContentView: View {
    @State var name = ""
    init() {
            //Use this if NavigationBarTitle is with Large Font
           UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]
           UINavigationBar.appearance().backgroundColor = .gray
        }
    var body: some View {
        NavigationView {
            
            ZStack{
                VStack{
                    TextField("Name", text: $name)
                        .frame(height:200)
                        .padding()
                        .background(backgrounImage())
                        .overlay(RoundedRectangle(cornerRadius: 20).stroke(Color.gray,lineWidth: 4))
                        .padding()
                    Spacer()
                }.navigationTitle("Tanvir")
                .background(Color.green.edgesIgnoringSafeArea(.all))
            }
            
        }
    }
}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    您可以在ZStack 之上添加另一个视图:

        var body: some View {
            NavigationView {
                ZStack(alignment: .top) { // <- Don't forget this
                    ,,,
     
                    GeometryReader { reader in
                        Color.yellow
                            .frame(height: reader.safeAreaInsets.top, alignment: .top)
                            .ignoresSafeArea()
                    }
                }
            }
        }
    

    不要忘记堆栈对齐!

    整个应用的一致栏

    如果您需要它出现在所有视图中,请尝试将代码放在更一致的位置,例如您提供 contentView 的位置:

    @main
    struct SwiftUIAppPlaygroundApp: App {
        var body: some Scene {
            WindowGroup {
                ZStack {
                    ContentView()
    
                    GeometryReader { reader in
                        Color.yellow
                            .frame(height: reader.safeAreaInsets.top, alignment: .top)
                            .ignoresSafeArea()
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 它正在工作,但我可以获取颜色视图的动态高度而不是给出近似值吗?
    • 更新为使用几何阅读器来获取准确的高度。
    • 是的,它正在工作。但是在每个屏幕中,我都需要设置 ZStack 和您的解决方案,这将是重复的。
    • 这完全是另一个问题,但别担心,看看更新的答案;)
    【解决方案2】:

    使用这个UIApplication 扩展来改变你的状态栏颜色

    extension UIApplication {
        /**
         Get status bar view
         */
        var statusBarUIView: UIView? {
            let tag = 13101996
            if let statusBar = self.windows.first?.viewWithTag(tag) {
                self.windows.first?.bringSubviewToFront(statusBar)
                return statusBar
            } else {
                let statusBarView = UIView(frame: UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame ?? .zero)
                statusBarView.tag = tag
                
                self.windows.first?.addSubview(statusBarView)
                return statusBarView
            }
        }
    }
    

    用法

    struct ContentViewStatusBar: View {
        @State var name = ""
        init() {
            //Use this if NavigationBarTitle is with Large Font
            UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]
            UINavigationBar.appearance().backgroundColor = .gray
        }
        var body: some View {
            NavigationView {
                
                ZStack{
                    VStack{
                        TextField("Name", text: $name)
                            .frame(height:200)
                            .padding()
                            .background(backgrounImage())
                            .overlay(RoundedRectangle(cornerRadius: 20).stroke(Color.gray,lineWidth: 4))
                            .padding()
                        Spacer()
                    }.navigationTitle("Tanvir")
                    .background(Color.green.edgesIgnoringSafeArea(.all))
                }
                
            }.onAppear {
                UIApplication.shared.statusBarUIView?.backgroundColor = .gray //<<=== Here
            }
        }
    }
    
    

    【讨论】:

    • 它工作正常,但是在 SwiftUI 2 中是否有更简单的解决方案?
    • 没有。状态栏没有什么特别的东西。
    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2021-02-12
    • 2021-02-11
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多