简体   繁体   English

iOS 15 SwiftUI TabView 标签栏外观不会在视图之间更新

[英]iOS 15 SwiftUI TabView tab bar appearance doesn't update between views

iOS 15 sets the TabView 's appearance depending on the loaded view's scroll position. iOS 15 根据加载视图的滚动位置设置TabView的外观。 However, this doesn't seem to update between views switched in the tab bar.但是,这似乎不会在选项卡栏中切换的视图之间更新。 How can I fix this so that the appearance updates properly?如何解决此问题以便外观正确更新?

  1. Opening a tabbed view without scrolling content ("no-scrolling view") uses a transparent background for the tab bar.在不滚动内容的情况下打开选项卡式视图(“无滚动视图”)使用透明背景作为选项卡栏。

  2. After navigating to a tabbed view with scrolling content ("scrolling view"), a translucent background is used.导航到带有滚动内容的选项卡式视图(“滚动视图”)后,将使用半透明背景。

  3. However, when coming back to the "no-scrolling view", the translucent background still remains instead of being replaced with a transparent background.然而,当回到“无滚动视图”时,半透明背景仍然存在,而不是被透明背景取代。

I did notice that the appearance updates properly when I open the Control Center or App Switcher and come back.我确实注意到,当我打开控制中心或应用程序切换器并返回时,外观会正确更新。

无滚动视图透明 滚动视图半透明 无滚动视图半透明

Reproduction:再生产:

enum Tab {
    case scroll
    case noScroll
}

struct ContentView: View {
    @State var selection: Tab = .noScroll
    
    var body: some View {
        TabView(selection: $selection) {     
            Text("Should have a transparent tab bar")
                .tabItem{ Label("No-scroll", systemImage: "xmark.circle") }
                .tag(Tab.noScroll)
            
            ScrollView {
                VStack(spacing: 10) {
                    ForEach(0..<100) {_ in
                        Text("Should have a translucent tab bar")
                    }
                }
            }
            .tabItem { Label("Scroll", systemImage: "circle") }
            .tag(Tab.scroll)
        }
    }
}

it's pretty much the same logic as @cole 's answer but here is my solution:这与@cole 的答案几乎相同,但这是我的解决方案:

.introspectTabBarController(customize: { controller in

            let appearance = controller.tabBar.standardAppearance
            appearance.configureWithDefaultBackground()
            appearance.backgroundColor = .red
            if #available(iOS 15.0, *) {
                controller.tabBar.scrollEdgeAppearance = appearance
            } else {
                controller.tabBar.standardAppearance = appearance
            }
        })

to use introspectTabBarController SwiftUI-Introspect使用 introspectTabBarController SwiftUI-Introspect

I had a similar issue here , solved by using this for iOS 15我在这里遇到了类似的问题,通过在 iOS 15 上使用它来解决

@available(iOS 15.0, *)
@NSCopying open var scrollEdgeAppearance: UITabBarAppearance?

Describes the appearance attributes for the tabBar to use when an observable scroll view is scrolled to the bottom.描述可观察滚动视图滚动到底部时要使用的 tabBar 的外观属性。 If not set, standardAppearance will be used instead.如果未设置,则将使用 standardAppearance。

//@available(iOS 15.0, *)
//@NSCopying open var scrollEdgeAppearance: UITabBarAppearance?

let appearance: UITabBarAppearance = UITabBarAppearance()
init() {
    UITabBar.appearance().scrollEdgeAppearance = appearance
}
.onAppear {
    if #available(iOS 15.0, *) {
        let appearance = UITabBarAppearance()
        UITabBar.appearance().scrollEdgeAppearance = appearance
    }
}

After update to XCode 13 & iOS 15 I also faced some TabView issues with bar background color and items text & icons color for different states.更新到 XCode 13 和 iOS 15 后,我还遇到了一些 TabView 问题,包括栏背景颜色和不同状态的项目文本和图标颜色。 The way I fixed it:我修复它的方式:

if #available(iOS 15, *) {
   let tabBarAppearance = UITabBarAppearance()
   tabBarAppearance.backgroundColor = backgroundColor
   tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
   tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
   tabBar.standardAppearance = tabBarAppearance
   tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
   UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
   UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
   tabBar.barTintColor = backgroundColor
 }

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

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