使用状态切换SF符号图标
Using state to switch SF symbol icons
如果用户点击照片,它会切换到具有以下代码的绘图:
import SwiftUI
struct TestView : View {
@State var photo = true
var imagePhoto = "Day-1"
var imageVector = "Dag-1"
var body: some View {
Image(photo ? imagePhoto : imageVector)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 200)
.background(Color.black)
.padding(.leading, 0)
.tapAction {
self.photo.toggle()
}
}
}
如果我想从 SF 符号更改图标来做同样的事情怎么办?以下代码无效。
Image(photo ? systemName: "photo" : systemName: "pencil.circle")
你用错了,这是正确的语法:
Image(systemName: photo ? "photo" : "pencil.circle")
如果用户点击照片,它会切换到具有以下代码的绘图:
import SwiftUI
struct TestView : View {
@State var photo = true
var imagePhoto = "Day-1"
var imageVector = "Dag-1"
var body: some View {
Image(photo ? imagePhoto : imageVector)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 200)
.background(Color.black)
.padding(.leading, 0)
.tapAction {
self.photo.toggle()
}
}
}
如果我想从 SF 符号更改图标来做同样的事情怎么办?以下代码无效。
Image(photo ? systemName: "photo" : systemName: "pencil.circle")
你用错了,这是正确的语法:
Image(systemName: photo ? "photo" : "pencil.circle")