简体   繁体   English

SwiftUI:点击更改 TectField 背景颜色

[英]SwiftUI: Change TectField Background Color on Tap

I'm new to SwiftUI and in learning mode.我是 SwiftUI 和学习模式的新手。

I'm working on code that will allow a user to select a date from a calendar.我正在研究允许用户从日历中获取 select 日期的代码。 Each day in the calendar is represented by a textfield.日历中的每一天都由一个文本字段表示。 I would like the selected day's textfield's background color to change when tapped.我希望在点击时更改所选日期的文本字段的背景颜色。 I'm able to change the background when the view first loads but, I have not been able to find a way to make it changes when tapped.我可以在视图首次加载时更改背景,但是我无法找到一种方法来使其在点击时更改。 Thanks for the help.谢谢您的帮助。

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)

    }

}

By logic of the code you need Text instead of TextField , so find below fixed & worked snapshot.根据代码的逻辑,您需要Text而不是TextField ,因此请在下面找到固定和工作的快照。

Tested with Xcode 11.4 / iOS 13.4使用 Xcode 11.4 / iOS 13.4 测试

演示

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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM