英文:
Swiftui Button to behave like a checkbox doesn't preview but works
问题
以下是复选框的代码,我不会提供代码翻译,只提供中文翻译部分:
这是我创建的复选框的代码。它在应用程序中运行良好,但在Xcode预览画布中不起作用。也就是说,在预览中点击它时,它不会改变状态。但当我在模拟器中运行应用程序并使用复选框时,它正常工作。有什么想法为什么会这样?
//
// CheckboxToggle.swift
//
//
//
import SwiftUI
enum CheckboxShape : String {
case circle = "circle"
case square = "square"
}
struct Checkbox: View {
@Binding var isChecked:Bool
var style:CheckboxShape
@State var action:(Bool)->() = {_ in }
var body: some View {
Button {
isChecked.toggle()
action(isChecked)
} label: {
let shape = style.rawValue
Image(systemName: isChecked ? "checkmark.\(shape).fill" : "\(shape)")
.resizable()
.frame(width: 24, height: 24)
.foregroundColor(isChecked ? .green : .gray)
.font(.system(size: 20, weight: .regular, design: .default))
}
.buttonStyle(PlainButtonStyle())
}
}
struct Checkbox_Previews: PreviewProvider {
@State static var isChecked:Bool = false
// 不确定为什么这个预览不起作用,但在其他视图中正常工作。
static var previews: some View {
HStack {
Checkbox(isChecked: $isChecked, style: .circle)
}
}
}
英文:
Here's the code for the Checkbox I created. It works great but in the Xcode preview canvas it doesn't work. As in, in the preview when you click it, it doesn't change. When I use the checkbox in my app and run it in the simulator it works great. Any ideas why this is?
//
// CheckboxToggle.swift
//
//
//
import SwiftUI
enum CheckboxShape : String {
case circle = "circle"
case square = "square"
}
struct Checkbox: View {
@Binding var isChecked:Bool
var style:CheckboxShape
@State var action:(Bool)->() = {_ in }
var body: some View {
Button {
isChecked.toggle()
action(isChecked)
} label: {
let shape = style.rawValue
Image(systemName: isChecked ? "checkmark.\(shape).fill" : "\(shape)")
.resizable()
.frame(width: 24, height: 24)
.foregroundColor(isChecked ? .green : .gray)
.font(.system(size: 20, weight: .regular, design: .default))
}
.buttonStyle(PlainButtonStyle())
}
}
struct Checkbox_Previews: PreviewProvider {
@State static var isChecked:Bool = false
// Not sure why this preview doens't work but the view works in other views fine.
static var previews: some View {
HStack {
Checkbox(isChecked: $isChecked, style: .circle)
}
}
}
答案1
得分: 1
SwiftUI的预览不以这种方式工作。
要使Checkbox
可交互,请将isChecked
声明为@State
@State var isChecked = false
并替换PreviewProvider为
struct Checkbox_Previews: PreviewProvider {
static var previews: some View {
Checkbox(style: .circle)
}
}
如果你想查看两种状态(同时保持@Binding
),并且保持Checkbox
的正方形外观,添加更多的Checkbox
视图并指定常量
struct Checkbox_Previews: PreviewProvider {
static var previews: some View {
HStack {
Checkbox(isChecked: .constant(true), style: .circle)
Checkbox(isChecked: .constant(false), style: .circle)
Checkbox(isChecked: .constant(true), style: .square)
Checkbox(isChecked: .constant(false), style: .square)
}
}
}
但是此预览不具有交互性。
英文:
SwiftUI previews don't work this way.
To make Checkbox
interactive declare isChecked
as @State
@State var isChecked = false
and replace the PreviewProvider with
struct Checkbox_Previews: PreviewProvider {
static var previews: some View {
Checkbox(style: .circle)
}
}
And if you want to see both states (and also the square appearance) and keep the @Binding
add more Checkbox
views and specify constants
struct Checkbox_Previews: PreviewProvider {
static var previews: some View {
HStack {
Checkbox(isChecked: .constant(true), style: .circle)
Checkbox(isChecked: .constant(false), style: .circle)
Checkbox(isChecked: .constant(true), style: .square)
Checkbox(isChecked: .constant(false), style: .square)
}
}
}
But this preview is not interactive.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论