【问题标题】:How would one update Button Image I.e. `Image(SystemName)` when pressed swiftUI如何更新按钮图像,即`Image(SystemName)` 按下 swiftUI
【发布时间】:2021-05-31 08:52:58
【问题描述】:

所以我是 Swift 的新手,这可能是一个相当愚蠢的问题。但是,我正在寻找一种在用户按下按钮时更新按钮Image(systemName) 的方法。我在网上找到了一些例子,但我很难掌握UIButtonButton 的概念。

我的当前按钮

// lock State (Set in struct ContentView: View)
@State var isViewLocked:Bool = false

// View Lock Button
Button(action: {
    isViewLocked.toggle()
}, label: {
    Image(systemName: "lock")
        .font(.system(size: 25,
            weight: .regular,
            design: .default))
        .foregroundColor(selectedIndex == number ? Color(.systemBlue) : Color(UIColor.lightGray))
})

提前致谢。

【问题讨论】:

    标签: xcode swiftui


    【解决方案1】:

    没关系,我想通了。 ?

    // lock State (Set in struct ContentView: View)
    @State var isViewLocked:Bool = false
    
    // View Lock Button
    Button(action: {
        self.isViewLocked.toggle()
    }, label: {
        Image(systemName: self.isViewLocked ? "lock.slash" : "lock")
            .font(.system(size: 25,
                weight: .regular,
                design: .default))
            .foregroundColor(selectedIndex == number ? Color(.systemBlue) : Color(UIColor.lightGray))
    })
    

    【讨论】:

    • 不需要self.isViewLocked == true 只需使用self.isViewLocked ? "lock.slash" : "lock"
    • 没错,我已经更新了我的答案。 +1
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多