jsoneaday
(Jsoneaday)
October 14, 2021, 1:03pm
1
I have this very simple code below and I am using the currentMenu to set the current tab's proper icon. But neither the button action nor the onTapGesture are executing as I see no print messages at all. It seems taps inside of tabItem are not registering.
NavigationView { }
.tabItem {
Button {
print("chat tapped")
env.currentMenu = DeChatMenus.PrivateDM
} label: {
Image(env.currentMenu == DeChatMenus.PrivateDM ? "Chat.Sel" : "Chat")
}
}
.tag(DeChatMenus.PrivateDM)
jsoneaday
(Jsoneaday)
October 14, 2021, 1:08pm
2
I switched to using .task in SwifttUI 3.0 and that does respond. This feels wrong to me but whatever.
tera
October 14, 2021, 6:47pm
3
do you want to be notified of a tab change?
class Model: ObservableObject {
static let singleton = Model()
private init() {}
var selection = 0 {
didSet {
print("selection changed to \(selection)")
}
}
}
struct ContentView1: View {
@ObservedObject var model = Model.singleton
var body: some View {
TabView(selection: $model.selection) {
Text("hello world 1").tabItem { Image(systemName: "photo") }.tag(1)
Text("hello world 2").tabItem { Image(systemName: "camera") }.tag(2)
}
}
}
// --- or ----
struct ContentView2: View {
static var currentTab = 0
var selection = Binding<Int> {
currentTab
} set: {
print("tab changed to \($0)")
currentTab = $0
}
var body: some View {
TabView(selection: selection) {
Text("hello world 1").tabItem { Image(systemName: "photo") }.tag(1)
Text("hello world 2").tabItem { Image(systemName: "camera") }.tag(2)
}
}
}
he's saying you can't put a button in the tab item and you have not addressed that problem. I can only get a sf symbol to animate using a button. If I can't get a button in the tab item to work then I can't get the sf symbol to work. Any insight?
import SwiftUI
struct ContentView: View {
@State private var petCount = 0
@State private var heartSymbol = "heart.fill"
var body: some View {
Button {
petCount += 1
if heartSymbol == "heart.fill" {
heartSymbol = "heart"
}
else {
heartSymbol = "heart.fill"
}
} label: {
Label("", systemImage: heartSymbol)
}
.symbolRenderingMode(.multicolor)
.symbolEffect(.bounce, value: petCount)
.font(.largeTitle)
}
}
#Preview {
ContentView()
}