无法使我的状态栏颜色与SiwftUI中的UINavigationBar颜色匹配

时间:2020-10-14 10:39:12

标签: swiftui uinavigationbar statusbar swiftui-navigationview uinavigationbarappearance

我的问题似乎很简单。但是我只是花了几个小时寻找解决方案! 我只需要使状态栏的背景色与我的UINavigationBar背景色(此示例中为红色)相符即可。

Status bar color is not conform to the NavigationBar's color

我写了一个最小的代码示例来重现我的问题,并帮助您为我提供解决方案...

TestApp.swift

import SwiftUI

@main
struct TestApp: App {
    @Environment(\.scenePhase) private var phase

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: phase) { newPhase in
            switch newPhase {
            case .active:
                UINavigationBar.appearance().backgroundColor = UIColor.red
                break
            default:
                break
            }
        }
    }
}

ContentView.swift

struct DetailView1: View {

    var body: some View {
        Text("Detail view 1")
    }
}

struct ContentView: View {
    @State private var isDisplayDetailView1 = true

    var body: some View {
        NavigationView {
            List {
                Section {
                    NavigationLink(destination: DetailView1(), isActive: $isDisplayDetailView1) {
                        Text("Go to detail view 1")
                    }
                }
            }
            .navigationBarTitle("Menu")
            .listStyle(GroupedListStyle())
        
            DetailView1()
        }
    }
}

很显然,我尝试将edgesIgnoringSafeArea(.top)添加到我的NavigationView视图(以及ListDetailView1视图中,...),但是什么都不会改变...请帮助我,我不知道问题出在哪里!

1 个答案:

答案 0 :(得分:0)

您使用procedure TForm2.TCPClientStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string); var Client: TIdTCPClient; IO: TIdIOHandler; begin Client := nil; if ASender is TIdTCPClient then begin Client := TIdTCPClient(ASender); end else if ASender is TIdIOHandler then begin IO := TIdIOHandler(ASender); if IO.Owner is TIdTCPClient then Client := TIdTCPClient(IO.Owner); end; if Client <> nil then begin // use Client.Tag as needed... end; end;

是有原因的吗?

您只需在ScenePhase上添加此代码

file scope
相关问题