SwiftUI - 设置状态栏背景颜色以与导航栏对齐

Han*_*eer 5 statusbar uinavigationbar ios swift swiftui

我正在尝试设置状态栏的背景颜色以使其与导航栏颜色对齐。在 UIKit 中,我会在它下面放置一个视图。在 SwiftUI 中,我尝试使用 aZStack 但是大标题不再起作用。

所以这是我目前没有绿色状态栏的工作状态:

 var body: some View {
        NavigationView {
            
            ScrollView {
                Text("Lol 123 Lol")
                    .foregroundColor(Color.secondary)
                    .padding([.top, .bottom], 16)
                    .padding([.leading,.trailing], 16)
                TwoTextFieldsView(isSecondSecure: true,
                                  firstTextFieldText: username,
                                  secondTextFieldText: password,
                                  firstTextFieldPlaceholder: "Username",
                                  secondTextFieldPlaceholder: "Password")
                    .padding([.leading,.trailing, .bottom], 16)
                
                
                
            }
            .navigationBarTitle("Connect to Lol")
            .onTapGesture {
                self.hideKeyboard()
            }
            
        }
        
    }
Run Code Online (Sandbox Code Playgroud)

它看起来像:

这个

Nik*_*kis 1

你必须添加

.edgesIgnoringSafeArea(.all)

修改器在你的背景颜色下。

struct ContentView: View {
    @State private var password = ""
    
    var body: some View {
        NavigationView {
            ZStack {
                
                Color.green
                    .edgesIgnoringSafeArea(.all) //<-- Add this modifier to the background Color
                
                VStack {
                    ScrollView {
                        Text("Lol 123 Lol")
                            .foregroundColor(Color.secondary)
                            .padding([.top, .bottom], 16)
                            .padding([.leading,.trailing], 16)
                        TextField("Test", text: $password)
                            .textFieldStyle(RoundedBorderTextFieldStyle())
                            .padding([.leading,.trailing, .bottom], 16)
                    }
                    .navigationBarTitle("Connect to Lol")
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。