SwiftUI TabView 不会不更新 UI


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()
}
}

我不知道为什么它不改变我的图像在选项卡视图

您遇到的问题很简单。你在和人机界面指南战斗。Apple希望您使用填充符号来表示TabViews,因此,无论您是否指定.fill, SwiftUI都会提供.fill

您可以使用.environment()来更改此行为。symbolvariables, _:)’修改器,但我不知道你是否会通过App Review。你可以这样使用它:

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)
}

你需要在三元的两边都使用它,否则你会得到不匹配的类型错误。

最后,如果您提供最小可重现示例(MRE),则诊断和调试代码会容易得多。

这段代码适合我:

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))")
}
}

相关内容

  • 没有找到相关文章

最新更新