更改自定义 UINavigationBar 的高度? (使用 Swift 或 SwiftUI)

Changing custom UINavigationBar's height? (Using Swift or SwiftUI)

我想给 UINavigationBar 一个静态高度,但我不太确定该怎么做。目前,我有一个 UINavigationBar 视图修饰符,如下所示:

struct NavigationBarModifier: ViewModifier {

var backgroundColor: UIColor?
var titleColor: UIColor?
var largeTitleColor: UIColor?
var tintColor: UIColor?

    init(backgroundColor: UIColor?, titleColor: UIColor?, largeTitleColor: UIColor?, tintColor: UIColor?) {
        self.backgroundColor = backgroundColor
        self.titleColor = titleColor
        self.largeTitleColor = largeTitleColor
        self.tintColor = tintColor
        let coloredAppearance = UINavigationBarAppearance()
        coloredAppearance.configureWithTransparentBackground()
        coloredAppearance.backgroundColor = .clear
        coloredAppearance.titleTextAttributes = [.foregroundColor: titleColor]
        coloredAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
    
        UINavigationBar.appearance().standardAppearance = coloredAppearance
        UINavigationBar.appearance().compactAppearance = coloredAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
        UINavigationBar.appearance().tintColor = tintColor
    }

    func body(content: Content) -> some View {
        ZStack{
            content
            VStack {
                GeometryReader { geometry in
                    Color(self.backgroundColor ?? .clear)
                        .frame(height: geometry.safeAreaInsets.top)
                        .edgesIgnoringSafeArea(.top)
                    Spacer()
                }
            }
        }
    }
}

我希望能够设置我为其创建初始化程序的导航栏的高度,但我不太确定如何设置。我尝试做一些 UINavigationBar.appearance() 修饰符,但那些没有用

据我所知,无法更改导航栏的高度。也许使用 UIKit 你可以做一些明确的框架操作,但通常我会建议不要这样做,因为它可能会扰乱你的视图布局。我通常发现有用的是通过外观移除导航栏下方的深色阴影线,并将内容放在导航栏下方,例如,与导航栏具有相同颜色的内容,从而从导航栏创建无缝过渡到它下面的视图。例如:

struct MyView: View {

    init() {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.shadowImage = UIImage()
        navBarAppearance.shadowColor = .clear
        navBarAppearance.backgroundColor = UIColor.red
        UINavigationBar.appearance().standardAppearance = navBarAppearance
        UINavigationBar.appearance().compactAppearance = navBarAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
    }

    var body: some View {
        NavigationView {
            VStack {
                Rectangle().foregroundColor(Color(UIColor.red)).frame(height: 100)
                Spacer()
            }
        }
    }
}