[英]TabView in SwiftUI how to deselect all tab items
是否有可能取消选择选项卡视图中的所有选项卡项。 或者也许我应该以某种方式更改所选项目的颜色以使其看起来没有突出显示
应该发生的情况是 model.selection >= 5
TabView(selection: $model.selection) {
更新
更多代码:
struct HamburgerTabView: View {
@EnvironmentObject var model: HamburgerMenuModel
var body: some View {
TabView(selection: $model.selection) {
NavigationView {
VStack {
Text("Lol").foregroundColor(.black)
NavigationLink(destination: Text("NAV").hamburgerButton() ) {
Text("Test").foregroundColor(.black)
}
}
.navigationBarTitle("Welcome", displayMode: .inline)
.hamburgerButton()
}
.tabItem {
VStack {
Image(systemName: "1.circle")
Text("Item 1")
}
}.tag(0)
NavigationView {
Text("Hello World 2").foregroundColor(.red)
.navigationBarTitle("Test 2", displayMode: .inline)
.hamburgerButton()
}
.tabItem {
VStack {
Image(systemName: "2.circle")
Text("Item 2")
}
}.tag(1)
NavigationView {
Text("Hello World 3")
.navigationBarTitle("Test 3", displayMode: .inline)
.hamburgerButton()
}
.tabItem {
VStack {
Image(systemName: "3.circle")
Text("Item 3")
}
}.tag(2)
NavigationView {
Text("Hello World 4")
.navigationBarTitle("Test 4", displayMode: .inline)
.hamburgerButton()
}
.tabItem {
VStack {
Image(systemName: "4.circle")
Text("Item 4")
}
}.tag(3)
NavigationView {
Text("Hello World 5")
.navigationBarTitle("Test 4", displayMode: .inline)
.hamburgerButton()
}
.tabItem {
VStack {
Image(systemName: "5.circle")
Text("Item 5")
}
}.tag(4)
}
.onAppear() {
//UITabBar.appearance().backgroundColor = .red
//UITabBar.appearance().tintColor = (self.model.selection < 5) ? .green : .red
}
.accentColor( (self.model.selection < 5) ? Color.black : Color.black.opacity(0.5))
}
}
所以我找到了通过扩展来修改它的最简单的解决方案:
extension UITabBarController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let appearance = UITabBarAppearance()
appearance.backgroundColor = .white
appearance.shadowImage = UIImage()
appearance.shadowColor = .white
appearance.stackedLayoutAppearance.normal.iconColor = .black
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
appearance.stackedLayoutAppearance.selected.iconColor = .red
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
tabBar.standardAppearance = appearance
}
}
应该有更好的选择,但我只能在视图的构造函数中找到修改一些东西,但它是有限的:
init() {
UITabBar.appearance().backgroundColor = UIColor.blue
}
我希望这有帮助!
编辑
这是我从自定义NavigationView
制作的基本自定义TabView
:
struct TabViewItem<E: Comparable, Content>: View where Content: View {
var id: E
@Binding var selected: E
let content: () -> Content
@inlinable public init(id: E, selected: Binding<E>, @ViewBuilder content: @escaping () -> Content) {
self.id = id
self._selected = selected
self.content = content
}
var body: some View {
HStack {
Spacer()
self.content()
Spacer()
}
.padding()
.background(self.selected == self.id ? Color(UIColor.systemGray5) : Color.clear)
.contentShape(Rectangle())
.onTapGesture {
self.selected = self.id
}
}
}
enum Tab : Int, Comparable {
public static func < (a: Tab, b: Tab) -> Bool {
return a.rawValue < b.rawValue
}
case Home, World, Settings
}
struct ContentView: View {
@State var selection: Tab = .Home
func containedView() -> AnyView {
switch self.selection {
case .Home: return AnyView(Text("Home"))
case .World: return AnyView(Text("World"))
case .Settings: return AnyView(Text("Settings"))
}
}
var body: some View {
GeometryReader { proxy in
VStack {
Spacer()
self.containedView()
Spacer()
HStack(alignment: .bottom, spacing: 0) {
TabViewItem(id: Tab.Home, selected: self.$selection, content: {
VStack {
Image(systemName: "house")
Text("Home")
}
})
TabViewItem(id: Tab.World, selected: self.$selection, content: {
VStack {
Image(systemName: "globe")
Text("World")
}
})
TabViewItem(id: Tab.Settings, selected: self.$selection, content: {
VStack {
Image(systemName: "gear")
Text("Settings")
}
})
}.frame(width: proxy.size.width, height: 50)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.