SwiftUI Tab Bar会将其后面的内容进行模糊处理。

huangapple go评论75阅读模式
英文:

SwiftUI Tab Bar that blurs content behind it

问题

I'm here to help with the translation. Here's the translated code portion you provided:

我正在尝试模糊底部标签栏后面出现的内容,以实现半透明/玻璃效果。

这是我想要实现的效果(来自 Figma):

 Twitter 中的标签栏类似,是透明的,但在标签栏后面的所有内容都变得模糊。

这是我的自定义标签栏的代码:

```swift
import SwiftUI

struct CustomTabBar: View {
    @Binding var currentTab: Tab
    
    var body: some View {
        GeometryReader{proxy in
            HStack(spacing: 0){
                ForEach(Tab.allCases,id: \.rawValue){tab in
                    Button{
                        withAnimation(.easeInOut(duration: 0.2)){
                            currentTab = tab
                        }
                    } label: {
                        Image(tab.rawValue)
                            .renderingMode(.template)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 30, height: 30)
                            .frame(maxWidth: .infinity)
                            .foregroundColor(currentTab == tab ? Color.white : Color.white.opacity(0.75))
                    }
                }
            }
            .frame(maxWidth: .infinity)
        }
        .frame(height: 30)
        .padding(.bottom, 10)
        .padding([.horizontal, .top])
        .background{
            Color.white.opacity(0.50)
                .ignoresSafeArea()
        }
    }
}

struct CustomTabBar_Previews: PreviewProvider {
    static var previews: some View {
        MainTabView()
    }
}

这是我的主标签视图代码,它是一个 ZStack,包含不同的视图和底部的自定义标签栏:

import SwiftUI

struct MainTabView: View {
    // 隐藏原生标签栏
    init(){
        UITabBar.appearance().isHidden = true
    }
    
    @State var currentTab: Tab = .dashboard
    
    var body: some View {
        ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)) {
            TabView(selection: $currentTab){
                ContentView()
                    .tag(Tab.dashboard)
                Text("Analytics")
                    .applyBG()
                    .tag(Tab.analytics)
                Text("Settings")
                    .applyBG()
                    .tag(Tab.settings)
            }

            // 自定义标签栏
            CustomTabBar(currentTab: $currentTab)
        }
    }
}

struct MainTabView_Previews: PreviewProvider {
    static var previews: some View {
        MainTabView()
    }
}

// 每个标签的背景颜色 - 在构建视图后可以删除
extension View{
    func applyBG()->some View{
        self
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background{
                Color("BG")
                    .ignoresSafeArea()
            }
    }
}

如果需要更多帮助,请随时提出问题。

英文:

I am trying to blur the content that appears behind my tab bar at the bottom so that it has a translucent / glassmorphism effect.

This is what I am trying to achieve (from Figma):

SwiftUI Tab Bar会将其后面的内容进行模糊处理。

Similar to how the tab bar in Twitter is transparent but everything that goes behind the tab bar becomes blurry.

This is the code for my Custom Tab Bar:

import SwiftUI

struct CustomTabBar: View {
    @Binding var currentTab: Tab
    
    var body: some View {
        GeometryReader{proxy in
            HStack(spacing: 0){
                ForEach(Tab.allCases,id: \.rawValue){tab in
                    Button{
                        withAnimation(.easeInOut(duration: 0.2)){
                            currentTab = tab
                        }
                    } label: {
                        Image(tab.rawValue)
                            .renderingMode(.template)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 30, height: 30)
                            .frame(maxWidth: .infinity)
                            .foregroundColor(currentTab == tab ? Color.white : Color.white.opacity(0.75))
                    }
                }
            }
            .frame(maxWidth: .infinity)
        }
        .frame(height: 30)
        .padding(.bottom, 10)
        .padding([.horizontal, .top])
        .background{
            Color.white.opacity(0.50)
                .ignoresSafeArea()
        }
    //END OF VAR BODY
    }
//END OF STRUCT
}

struct CustomTabBar_Previews: PreviewProvider {
    static var previews: some View {
        MainTabView()
    }
}

This is my code for the Main Tab View that is a ZStack with my different views and the custom tab bar at the bottom:

import SwiftUI

struct MainTabView: View {
    //HIDING NATIVE TAB BAR
    init(){
        UITabBar.appearance().isHidden = true
    }
    
    @State var currentTab: Tab = .dashboard
    
    var body: some View {
        ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)) {
            TabView(selection: $currentTab){
                ContentView()
                    .tag(Tab.dashboard)
                Text("Analytics")
                    .applyBG()
                    .tag(Tab.analytics)
                Text("Settings")
                    .applyBG()
                    .tag(Tab.settings)
            }

            //CUSTOM TAB BAR
            CustomTabBar(currentTab: $currentTab)
        }
    //END OF VAR BODY
    }
//END OF STRUCT
}

struct MainTabView_Previews: PreviewProvider {
    static var previews: some View {
        MainTabView()
    }
}

//BACKGROUND COLOR OF EACH TAB -- CAN REMOVE ONCE YOU BUILD OUT THE VIEWS
extension View{
    func applyBG()->some View{
        self
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background{
                Color("BG")
                    .ignoresSafeArea()
            }
    }
}

I have tried using the solution from this overflow question

But when I do this it blurs the entire view behind the tab bar, not just the content behind the tab bar:

SwiftUI Tab Bar会将其后面的内容进行模糊处理。

The colors are different here, but same principal applies.

Any idea how I can achieve this blurred look behind JUST the tab bar?

答案1

得分: 0

你当前似乎将标签栏的背景设置为半透明的白色,但对于模糊效果,您可能需要使用其中一种材料,如.ultraThinMaterial

英文:

It seems like you currently have your tab bar's background to be a half-opaque white, but for blur effects, you'd want one of the materials, such as .ultraThinMaterial

huangapple
  • 本文由 发表于 2023年4月19日 23:26:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定