import SwiftUI
import UIKit
import Foundation
struct ContentView: View {
@State private var tabSelection = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
TabView(selection: $tabSelection) {
ListCard(isPreferitiSection: false)
.tabItem {
tabSelection == 0 ? Label("Home", systemImage: "house.fill") : Label("Home", systemImage: "house")
}
.tag(0)
ListCard(isPreferitiSection: true)
.tabItem {
tabSelection == 1 ? Label("Preferiti", systemImage: "bookmark.fill") : Label("Preferiti", systemImage: "bookmark")
}
.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'm not getting a handle on why it doesn't change my image within the tab view
CodePudding user response:
The problem you are having is simple. You are fighting the Human Interface Guides. Apple wants you to use filled symbols for TabViews
, so, regardless of whether you designate a .fill
or not, SwiftUI will supply a .fill
.
You can change this behavior by using the .environment(.symbolVariants, _:)` modifer, but I don't know if you would pass App Review. You would use it like this:
TabView(selection: $tabSelection) {
ListCard(isPreferitiSection: false)
.tabItem {
tabSelection == 0 ? Label("Home", systemImage: "house").environment(\.symbolVariants, .fill) : Label("Home", systemImage: "house").environment(\.symbolVariants, .none)
}
.tag(0)
ListCard(isPreferitiSection: true)
.tabItem {
tabSelection == 1 ? Label("Preferiti", systemImage: "bookmark").environment(\.symbolVariants, .fill) : Label("Preferiti", systemImage: "bookmark").environment(\.symbolVariants, .none)
}
.tag(1)
}
You need to use it on both sides of the ternaries, or you will get mismatched type errors.
Lastly, it is much easier to diagnose and debug code if your provide a Minimal Reproducible Example (MRE).
CodePudding user response:
This code works for me:
import SwiftUI
import UIKit
import Foundation
struct ContentView: View {
@State private var tabSelection = 0
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
TabView(selection: $tabSelection) {
ListCard(isPreferitiSection: false)
.tabItem {
tabSelection == 0 ? Label("Home", systemImage: "house.fill") : Label("Home", systemImage: "house")
}
.tag(0)
ListCard(isPreferitiSection: true)
.tabItem {
tabSelection == 1 ? Label("Preferiti", systemImage: "bookmark.fill") : Label("Preferiti", systemImage: "bookmark")
}
.tag(1)
}
}
}
struct ListCard: View {
var isPreferitiSection = false
init(isPreferitiSection: Bool){
self.isPreferitiSection = isPreferitiSection
}
var body: some View {
Text("isPreferitiSection is \(String(isPreferitiSection))")
}
}