[英]SwiftUI Tabbar disappears when List touches Tabbar
I'm trying to create a List from a TabView using SwiftUI.我正在尝试使用 SwiftUI 从 TabView 创建一个列表。 But when the List touches the tabbar, the tabbar becomes transparent.
但是当 List 接触到 tabbar 时,tabbar 变成透明的。 If I use ".frame" to limit the size of the List so that the List doesn't touch the tabbar, it works properly.
如果我使用“.frame”来限制列表的大小,以便列表不接触标签栏,它可以正常工作。
Why does tabbar become transparent?为什么tabbar会变透明? and how can I make the tabbar not become transparent?
以及如何使标签栏不透明?
target ios is 15.0 and xcode version is 13.4目标 ios 是 15.0 和 xcode 版本是 13.4
Here is my code and image.这是我的代码和图像。
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()
}
}
}
}
A possible approach is to construct appearance explicitly with all states needed (so be sure everything is activated and not conflicting with others), like一种可能的方法是用所有需要的状态显式地构造外观(所以要确保一切都被激活并且不与其他状态冲突),比如
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
}
Tested with Xcode 13.4 / iOS 15.5使用 Xcode 13.4 / iOS 15.5 测试
You forgot to add this:)你忘了添加这个:)
UITabBar.appearance().barTintColor = UIColor(Color("mainColor"))
Have a great day!祝你有美好的一天!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.