Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI:点击时更改TectField背景色_Swiftui_Textfield_Background Color - Fatal编程技术网

SwiftUI:点击时更改TectField背景色

SwiftUI:点击时更改TectField背景色,swiftui,textfield,background-color,Swiftui,Textfield,Background Color,我刚接触SwiftUI,处于学习模式 我正在编写允许用户从日历中选择日期的代码。日历中的每一天都由一个文本字段表示。我希望在点击时更改所选日期文本字段的背景色。我可以在视图首次加载时更改背景,但是,我无法找到在点击时进行更改的方法。谢谢你的帮助 import SwiftUI var selectDay: Int = 0 struct ContentView: View { @State var strDays = ["01","02","03","04","05","06","07

我刚接触SwiftUI,处于学习模式

我正在编写允许用户从日历中选择日期的代码。日历中的每一天都由一个文本字段表示。我希望在点击时更改所选日期文本字段的背景色。我可以在视图首次加载时更改背景,但是,我无法找到在点击时进行更改的方法。谢谢你的帮助

import SwiftUI

var selectDay: Int = 0

struct ContentView: View {

    @State var strDays = ["01","02","03","04","05","06","07"]

    var body: some View {

        VStack (spacing:10) {

            HStack (spacing: 2) {

                TextField("", text: $strDays[0])
                    .modifier(dayModifier())
                    .background(getBackgroundColor(thisDay: 0))
                    .onTapGesture {
                        self.selectDate(selection: 0)
                 }

                TextField("", text: $strDays[1])
                    .modifier(dayModifier())
                    .background(getBackgroundColor(thisDay: 1))
                    .onTapGesture {
                        self.selectDate(selection: 1)

                }

            }

        }

    }

    func selectDate(selection: Int) {
        strDays[selection] = "99"
        selectDay = selection
    }

    func getBackgroundColor(thisDay: Int) -> Color {

        var backgroundColor = Color.clear

        if selectDay == thisDay {
            backgroundColor = Color.blue
        }

    return backgroundColor

    }

struct dayModifier: ViewModifier {

     func body(content: Content) -> some View {

        return content
            .frame(width: 50.0, height: 50)
            .multilineTextAlignment(.center)
            .foregroundColor(.primary)
            .border(Color.primary, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)

            .disabled(true)

    }

}

根据代码逻辑,您需要的是
Text
而不是
TextField
,因此请查看下面的修复和工作快照

使用Xcode 11.4/iOS 13.4进行测试


根据代码逻辑,您需要的是
Text
而不是
TextField
,因此请查看下面的修复和工作快照

使用Xcode 11.4/iOS 13.4进行测试


谢谢你的帮助,阿斯佩里。有趣的是,它在我第一次点击文本时起作用,但在随后的点击中不起作用。我必须按如下方式更改selectDate函数,以便在每次点击时更改背景:func selectDate(selection:Int){strDays[selection]=“strDays[selection]=“0”+String(selection+1)selectDay=selection}感谢您的帮助Asperi。有趣的是,它在我第一次点击文本时起作用,但在随后的点击中不起作用。我必须按如下方式更改selectDate函数,以便在每次点击时更改背景:func selectDate(selection:Int){strDays[selection]=“strDays[selection]=”strDays[selection]=“0”+String(selection+1)selectDay=selection}
struct ContentView: View {

    @State var selectDay: Int = 0
    @State var strDays = ["01","02","03","04","05","06","07"]

    var body: some View {
        VStack (spacing:10) {
            HStack (spacing: 2) {
                Text(strDays[0])
                    .modifier(dayModifier())
                    .background(getBackgroundColor(thisDay: 0))
                    .onTapGesture {
                        self.selectDate(selection: 0)
                }

                Text(strDays[1])
                    .modifier(dayModifier())
                    .background(getBackgroundColor(thisDay: 1))
                    .onTapGesture {
                        self.selectDate(selection: 1)

                }
            }
        }
    }

    func selectDate(selection: Int) {
        strDays[selection] = "99"
        selectDay = selection
    }

    func getBackgroundColor(thisDay: Int) -> Color {
        var backgroundColor = Color.clear
        if selectDay == thisDay {
            backgroundColor = Color.blue
        }
        return backgroundColor

    }

    struct dayModifier: ViewModifier {
        func body(content: Content) -> some View {
            content
                .frame(width: 50.0, height: 50)
                .multilineTextAlignment(.center)
                .foregroundColor(.primary)
                .border(Color.primary, width: 1)
        }
    }
}