我正在撕扯我的头发,试图弄清楚如何在我的 SwiftUI 视图中绑定选择的值:
选择器需要绑定到从标签返回的 Int。我需要将此 Int 转换为 String 并设置绑定。如何?
struct ContentView: View {
@Binding var operatorValueString:String
var body: some View {
Picker(selection: queryType, label: Text("Query Type")) {
ForEach(0..<DM.si.operators.count) { index in
Text(DM.si.operators[index]).tag(index)
}
}.pickerStyle(SegmentedPickerStyle())
}
}
Run Code Online (Sandbox Code Playgroud)
如何以及在哪里设置我的 operatorValueString ?
operatorValueString = DM.si.operators[queryType] //不会编译。
我想为buttonStyle明暗模式的按钮设置自定义修饰符。如何根据浅色或深色模式更改 buttonStyle 修饰符?我想为我的按钮为明暗模式设置自定义修饰符。
这是我的按钮代码,
Button(action: {
print("button tapped")
}, label: {
LinearGradient(gradient: Gradient(colors: [.darkBlueColor, .lightBlueColor]), startPoint: .top, endPoint: .bottom)
.mask(Image(systemName: "ellipsis")
.resizable()
.aspectRatio(contentMode: .fit)
).frame(width: iPhoneSE ? 26 : 25, height: iPhoneSE ? 26 : 25, alignment: .center)
})
.buttonStyle(lightButtonStyle())
struct lightButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding(10)
.background(
Group {
if configuration.isPressed {
Circle()
.fill(Color.offWhite)
.overlay(
Circle()
.stroke(Color.lightGray2, lineWidth: 4)
.blur(radius: 1)
.offset(x: 2, y: 2)
.mask(Circle().fill(LinearGradient(Color.black, Color.clear)))
)
.overlay(
Circle()
.stroke(Color.white, …Run Code Online (Sandbox Code Playgroud) 我试图在输入TextField时更改Text的值。当我在TextField中键入任何内容时,我想使用Textfield值进行一些计算并将其显示在Text上。在Swift中,我们有UITextField的委托方法来跟踪UITextField的更改。func textFieldDidChange(_ textField: UITextField)
在SwiftUI中,我尝试使用创建TextField onEditingChanged。但不知何故,它并没有进入那个障碍。我不知道我是否做错了什么。
@State var totalAmount: String = ""
@State var addAmount: String = ""
var body: some View {
HStack {
TextField("0.0", text: $totalAmount, onEditingChanged: { _ in
self.addAmount = //doing some calculation with totalAmount
})
Text(self.addAmount)
}
}
Run Code Online (Sandbox Code Playgroud)
有人能帮忙吗?
在这里,我正在使用 Slider。我们可以accentColor像这样改变Slider ,
Slider(value: $tip, in: 0...50, step: 1)
.accentColor(.red)
Run Code Online (Sandbox Code Playgroud)
但是如何申请Gradient一个accentColor?
我试图通过屏蔽滑块来应用渐变,但后来我无法再滑动它了。
LinearGradient(gradient: Gradient(colors: [.red, .blue]), startPoint: .top, endPoint: .bottom)
.mask(Slider(value: $tip, in: 0...50, step: 1))
Run Code Online (Sandbox Code Playgroud)
那么有没有什么间接的方法呢?