SwiftUI中的选项卡视图如何取消选择所有选项卡项
是否可以取消选择选项卡视图中的所有选项卡项。 或者,也许我应该改变所选项目的颜色,使其看起来并没有突出显示 应该发生的情况是model.selection>=5SwiftUI中的选项卡视图如何取消选择所有选项卡项,swift,swiftui,Swift,Swiftui,是否可以取消选择选项卡视图中的所有选项卡项。 或者,也许我应该改变所选项目的颜色,使其看起来并没有突出显示 应该发生的情况是model.selection>=5 TabView(selection: $model.selection) { 更新 更多代码: struct HamburgerTabView: View { @EnvironmentObject var model: HamburgerMenuModel var body: some View {
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))
}
}
struct-HamburgerTabView:View{
@环境对象变量模型:HamburgerMinumodel
var body:一些观点{
选项卡视图(选择:$model.selection){
导航视图{
VStack{
文本(“Lol”).foregroundColor(.black)
NavigationLink(目的地:文本(“NAV”).hamburgerButton(){
文本(“测试”)。前底色(.黑色)
}
}
.navigationBarTitle(“欢迎”,显示模式:。内联)
.汉堡包()
}
.tabItem{
VStack{
图像(系统名称:“1.圆圈”)
案文(“项目1”)
}
}.tag(0)
导航视图{
文本(“Hello World 2”).foregroundColor(.red)
.navigationBarTitle(“测试2”,显示模式:。内联)
.汉堡包()
}
.tabItem{
VStack{
图像(系统名称:“2.圆圈”)
案文(“项目2”)
}
}.标签(1)
导航视图{
文本(“你好,世界3”)
.navigationBarTitle(“测试3”,显示模式:。内联)
.汉堡包()
}
.tabItem{
VStack{
图像(系统名称:“3.圆圈”)
案文(“项目3”)
}
}.标签(2)
导航视图{
文本(“你好,世界4”)
.navigationBarTitle(“测试4”,显示模式:。内联)
.汉堡包()
}
.tabItem{
VStack{
图像(系统名称:“4.圆圈”)
案文(“项目4”)
}
}.标签(3)
导航视图{
文本(“Hello World 5”)
.navigationBarTitle(“测试4”,显示模式:。内联)
.汉堡包()
}
.tabItem{
VStack{
图像(系统名称:“5.圆圈”)
案文(“项目5”)
}
}.标签(4)
}
.onAppear(){
//UITabBar.appearance().backgroundColor=.red
//uitabar.appearance().tintColor=(self.model.selection<5)?。绿色:。红色
}
.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
}
我希望这有帮助
编辑
这里有一个基本的自定义选项卡视图
,我使用自定义导航视图
:
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)
}
}
}
}
struct TabViewItem:View其中Content:View{
变量id:E
@所选绑定变量:E
让内容:()->内容
@可插入的公共init(id:E,selected:Binding,@ViewBuilder内容:@escaping()->content){
self.id=id
self.\u selected=已选择
self.content=内容
}
var body:一些观点{
HStack{
垫片()
self.content()
垫片()
}
.padding()
.background(self.selected==self.id?Color(UIColor.systemGray5):Color.clear)
.contentShape(矩形())
.ontapsigne{
self.selected=self.id
}
}
}
枚举选项卡:Int,可比较{
公共静态函数<(a:Tab,b:Tab)->Bool{
返回a.rawValueAnyView{
开关自选{
case.Home:返回AnyView(文本(“Home”))
case.World:返回AnyView(文本(“World”))
case.Settings:返回AnyView(文本(“设置”))
}
}
var body:一些观点{
GeometryReader{中的代理
VStack{
垫片()
self.containedView()
垫片()
HStack(对齐:。底部,间距:0){
TabViewItem(id:Tab.Home,选定项:self.$selection,内容:{
VStack{
图像(系统名称:“房屋”)
文本(“主页”)
}
})
TabViewItem(id:Tab.World,选定项:self.$selection,内容:{
VStack{
图像(系统名称:“全球”)
文本(“世界”)
}
})
TabViewItem(id:Tab.Settings,选定项:self.$selection,内容:{
VStack{
图像(系统名称:“齿轮”)
文本(“设置”)
}
})
}.框架(宽度:proxy.size.width,高度:50)
}
}
}
}
您能分享更多您尝试过的代码吗?我添加了一些代码。但是TabView似乎有非常有限的定制选项,比如颜色?你可能是对的。我