问题描述
当我在 NavigationView
中添加 TabView
时,我在 SwiftUI 项目中看到了一个意外的 UI 问题。
这是我的代码,
struct MainView: View {
@State private var selectedTab: Int = 0
var body: some View {
TabView(selection: $selectedTab) {
NavigationView {
ScrollView {
vstack {
ForEach(0..<30) { i in
Text("Hello World")
.padding()
.frame(minWidth: .zero,maxWidth: .infinity)
}
}
}.background(Color.red)
.navigationTitle("Home View")
}.background(Color.green)
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
TabItem(title: "Explore",systemImage: selectedTab == 0 ? "house.fill" : "house")
}.tag(1)
ExploreView()
.background(Color.red)
.tabItem {
TabItem(title: "Explore",systemImage: selectedTab == 1 ? "safari.fill" : "safari")
}.tag(1)
}
}
}
这是我的场景委托代码,
struct RootView: View {
var body: some View {
MainView()
}
}
class SceneDelegate: UIResponder,UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,willConnectTo _: UIScenesession,options _: UIScene.Connectionoptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
self.window = window
}
let host = UIHostingController(rootView: RootView())
window?.rootViewController = host
window?.makeKeyAndVisible()
}
}
ScrollView
(位于 NavigationView
内部)的高度没有占据全屏。我尝试了所有技巧(填充,框架,GeometryReader
,...),但没有运气。我不知道我在这里做错了什么,任何帮助将不胜感激。
解决方法
它在安全区域内,你需要忽略它
}.background(Color.red)
.navigationTitle("Home View")
.ignoresSafeArea() // << here !!
,
感谢您的回答并提供帮助。
我不小心评论了 UITabBar
的外观逻辑,哪个解决了这个问题。我不确定这是 SwiftUI 中的错误还是我遗漏了什么。无论如何,评论以下外观解决了问题,
// UITabBar.appearance().isTranslucent = false <<-- Remove this