SwiftUI 导航栏 NavigationView

前言

xcode 13.3
iOS 15.2

NavigationView: 标题、展示模式、隐藏导航栏、隐藏返回按钮、添加导航栏按钮

1、隐藏导航栏

1.1、使用ignoresSafeArea() 调整安全区域

		.ignoresSafeArea() 

1.2、隐藏导航栏

        .navigationTitle("")
        .navigationBarHidden(true)

如果不添加此处,顶部会有UINavigationBar透明视图挡着你的视图
而且在Preview上运行正常,真机和模拟器出现被遮盖
在这里插入图片描述
整体代码

init() {
    
    
    UITableView.appearance().backgroundColor = .clear
}
 
var body: some View {
    
    
    NavigationView {
    
    
        VStack {
    
    
            MineHeader()
            VStack(alignment: .center, spacing: 10) {
    
    
                Spacer()
                    .frame(height: 5)
                ForEach(models.indices, id: \.self) {
    
     index in
                    MineRow(model: models[index])
                }
                Spacer()
                    .frame(height: 5)
            }
            .background()
            .cornerRadius(10)
            .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
            .offset(y: -30)
            Spacer()
        }
        .ignoresSafeArea()
        .navigationTitle("")
        .navigationBarHidden(true)
        .background(Color(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0))
    }
}

2、 隐藏导航栏返回按钮

.navigationBarBackButtonHidden(true)

3、添加导航栏按钮

.navigationBarItems(leading: Text("left"), trailing: Text("right"))

二、导航栏其他设置

NavigationView 只有tabbar的View有就可以了,如果跳转到下一页,再次添加NavigationView,就会造成一下效果,多了一个导航栏的高度
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u014651417/article/details/123556167