【问题标题】:Changing custom UINavigationBar's height? (Using Swift or SwiftUI)更改自定义 UINavigationBar 的高度? (使用 Swift 或 SwiftUI)
【发布时间】:2020-07-12 19:27:53
【问题描述】:

我想给 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() 修饰符,但没有奏效

【问题讨论】:

  • @raynok 我该怎么做呢?
  • 在下面查看我的答案。

标签: swift swiftui uinavigationbar navigationbar


【解决方案1】:

据我所知,无法更改导航栏的高度。也许使用 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()
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2014-10-16
    • 2010-10-28
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2012-08-15
    相关资源
    最近更新 更多