我刚接触Swift,尝试在SwiftUI中使用NavigationView创建一个简单的屏幕。但奇怪的是,当我将任何内容包裹在NavigationView中时,它会在底部添加额外的空间。我想知道是否有其他人也遇到了这个问题。
这是我的HomeView:
struct HomeView: View {
var body: some View {
NavigationView {
ZStack {
Color.surface.edgesIgnoringSafeArea(.all)
Text("HOME")
}
}
}
}
这是我的带有TabView的ContentView:
struct ContentView: View {
@EnvironmentObject var session: SessionStore
@State private var selected = 1
@State private var loaded: Bool = false
var ref: DatabaseReference! = Database.database().reference()
func getUser() {
//Promisify this
session.listen()
self.loaded = true
// Firebase test
self.ref.child("users").child("test").setValue(["username" : "TEST"])
}
// Sets the bottom tab background color
init(){
UITabBar.appearance().isTranslucent = false
UITabBar.appearance().barTintColor = UIColor(named: "card2")
}
var body: some View {
Group {
if (self.loaded == false){
Text("loading...")
}
else if (session.session != nil) {
TabView(selection: $selected) {
HomeView()
.tabItem {
Image(systemName: "music.house.fill")
Text("Home")
}
MyRoutinesView()
.tabItem({
Image(systemName: "music.note.list")
Text("My Routines")
}).tag(1)
MetronomeView()
.tabItem({
Image(systemName: "music.note")
Text("Tools")
}).tag(2)
SettingsView()
.tabItem({
Image(systemName: "gear")
Text("Settings")
}).tag(3)
}
//.background(Color.surface)
.accentColor(Color.white)
//.font(.headline)
} else if (self.loaded == true && session.session == nil) {
AuthView()
}
}.onAppear(perform: getUser)
}
}
// Gets colors from assets
extension Color {
static let primary = Color("primary")
static let secondary = Color("secondary")
static let surface = Color("surface")
static let card = Color("card")
static let cardShadow = Color("cardShadow")
static let card2 = Color("card2")
}
以下是当前的显示效果(问题出现在选项卡导航上面的空白处):
非常感谢您所提供的任何帮助!
HomeView
之前出现的View
。另外,您是否在某个地方使用了TabView
? - staticVoidManUITabBar.appearance().isTranslucent = false
导致问题。有趣的是……一定是SwiftUI的一个漏洞。 - staticVoidMan