简体   繁体   English

ios - 当 NavigationView 的内容太短时,SwiftUI 中的 TabView 会丢失背景

[英]ios - TabView in SwiftUI loses background when content of the NavigationView is too short

Having the code of my very simple SwiftUI app attached, I'm struggling with a visual issue:附上我非常简单的 SwiftUI 应用程序的代码后,我正在努力解决一个视觉问题:

  1. When navigating between pages inside of a TabView, the tab bar in the bottom has a nice translucent background and navigating forth and back is looking nice - if the subpages are taller than the screen size:在 TabView 内的页面之间导航时,底部的标签栏具有漂亮的半透明背景,并且来回导航看起来不错 - 如果子页面高于屏幕尺寸:

正确的

  1. When the second page (DetailView) has a content that is shorter than the screen size, the tab bar's background disappears and that causes a very annoying overlap effect when navigating back:当第二个页面(DetailView)的内容比屏幕尺寸短时,标签栏的背景会消失,这会在向后导航时导致非常烦人的重叠效果:

不正确

Is there a solution for this in SwiftUI, iOS 15? SwiftUI, iOS 15 中是否有解决方案?

Code:代码:

import SwiftUI

struct ContentView: View {
    @State private var isDetailActive = false

    var body: some View {
        TabView {
            NavigationView {
                ScrollView {
                    VStack {
                        Text("Custom title")
                            .frame(maxWidth: .infinity)
                            .padding(20)
                            .background(Color(red: 0.1, green: 0.1, blue: 0.1))
                    }

                    ForEach(1..<50) {_ in
                        NavigationLink(destination: DetailView(isVisible: $isDetailActive), isActive: $isDetailActive) {
                            Text("Hello, world!")
                                .padding(10)
                        }
                    }
                }
                .navigationBarHidden(true)
                .navigationTitle("Navigation title")
            }
            .tabItem {
                Image(systemName: "house")
                Text("Test")
            }
        }
    }
}

struct DetailView: View {
    @Binding var isVisible: Bool

    var body: some View {
        ScrollView {
            VStack {
                Button(action: {
                    isVisible = false
                }) {
                    Text("Back")
                        .frame(maxWidth: .infinity)
                        .padding(20)
                        .background(Color(red: 0.1, green: 0.1, blue: 0.1))
                }

                ForEach(0..<10) { item in       // <-- change this to 20 to simulate a larger page
                    Text("Text")
                        .padding(10)
                }
            }
        }
        .navigationBarHidden(true)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .colorScheme(.dark)
    }
}

iOS 15 changed the default look of the TabBar when there is nothing underneath it to scroll. iOS 15 更改了 TabBar 下方没有可滚动内容时的默认外观。

A work-around that worked for me is modifying the TabBar in an onAppear statement:对我有用的解决方法是在 onAppear 语句中修改 TabBar:

TabView {
    ...
}
.onAppear {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.configureWithDefaultBackground()
    UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM