我正在try 使用SwiftUI从TabView创建列表.但当列表接触到选项卡栏时,选项卡栏就会变得透明.如果我使用".Frame"来限制列表的大小,这样列表就不会接触到选项卡栏,它就能正常工作.

为什么选项卡栏会变得透明? 我如何才能使选项卡栏不变得透明呢?

目标iOS为15.0,Xcode版本为13.4

以下是我的代码和图像.

enter image description here

struct HomeView: View {
    
    @State private var selection = 1
    
    init(){
        UITabBar.appearance().backgroundColor = UIColor(Color("mainColor"))//UIColor.white
        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().unselectedItemTintColor = UIColor.white
        UITabBar.appearance().isHidden = false

    }
    
    var body: some View {
        VStack {
            TabView (selection: $selection){
                FeedView()
                    .tabItem {
                        Image(systemName : "person.2.circle")
                            .environment(\.symbolVariants, selection == 1 ? .fill : .none)
                        Text("피드")
                    }.tag(1)
                
                NMapView()
                    .tabItem {
                        Image(systemName : "map.circle")
                            .environment(\.symbolVariants, selection == 2 ? .fill : .none)
                        Text("지도")
                    }.tag(2)
                
                SearchView()//text: "")
                    .tabItem {
                        Image(systemName : "magnifyingglass.circle")
                            .environment(\.symbolVariants, selection == 3 ? .fill : .none)
                        Text("검색")
                    }.tag(3)
                
                SettingView()
                    .tabItem {
                        Image(systemName : "ellipsis.circle")
                            .environment(\.symbolVariants, selection == 4 ? .fill : .none)
                        Text("설정")
                    }.tag(4)
            }
            .accentColor(.white)
        }
        //.ignoresSafeArea(edges: .top)
    }
}
struct SearchView: View {
    let array = [
        "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "0", "one",
        "two", "three", "four", "five", "six"
    ]
    
    @State private var searchText = ""
    
    var body: some View {
        
        VStack {
            SearchBar(text: $searchText)
                .padding(EdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0))
            
            List {
                ForEach(array.filter{$0.hasPrefix(searchText)}, id:\.self) {
                    searchText in Text(searchText)
                }
            }
            .listStyle(PlainListStyle())
            .onTapGesture {
                hideKeyboard()
            }
        }
    }
}

推荐答案

一种可能的方法是使用所需的所有状态显式构造外观(因此确保所有状态都被激活,并且不与其他状态冲突),如

init() {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = UIColor.orange


    let itemAppearance = UITabBarItemAppearance(style: .stacked)
    itemAppearance.normal.iconColor = UIColor.white
    itemAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.stackedLayoutAppearance = itemAppearance

    UITabBar.appearance().scrollEdgeAppearance = appearance
    UITabBar.appearance().standardAppearance = appearance
}

使用Xcode 13.4/iOS 15.5进行测试

demo

Ios相关问答推荐

Firebase SDK 10+—无法安装软件包

如何在iPhone 15上消除.NET Maui中状态栏和页面之间的差距

当重新呈现UI时,嵌套的 struct 值如何与@Observable一起工作?

在嵌套的DISPATCH_AFTER块中正确使用strong自/弱自

如何在Swift/Combine中从timer.publish属性传递两个参数到任意方法

SwiftUI中的Tap手势无法识别视图上的偏移变化

在 SwiftUI 中,绑定应该放在 ViewModel 中吗?

SwiftUI 中的偏移量和按钮有问题吗?

类型任何视图不能符合具有泛型的协议上的视图

在 SwiftUI 中将 TextEditor 文本计数连接到 ProgressView

如何在字段包含字典数组的firestore上查询?

在 Swift 中为 UITextField/UIView 摇动动画

UIScrollview 获取touch 事件

如何在 UIActivityViewController 中设置邮件主题?

iOS 应用程序中的手动语言 Select (iPhone 和 iPad)

正确使用 Alamofire 的 URLRequestConvertible

如何使用完成按钮制作 UIPickerView?

用 NavigationViewController 快速呈现 ViewController

iOS 中的 Google Analytics SDK 3.0 _sqlite3 链接器错误

如何创建 Select 器数组