Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I cancel the text field editing. I want no other editing on the TextField when the selection is made. I tried it myself, but I couldn't.

struct profile: View {
    @State private var iliskiDurumlari: [String] = ["Evli", "Bekar", "Ayr?", "anan"]
    @State private var iliskiVisible = false
    @State private var iliski = ""

    var body: some View {
        VStack {
            TextField("?li?ki Durumu Se?iniz..", text: $iliski)
                .frame(width: 300, height: 50, alignment: .center)
                .padding(5)
                .font(Font.system(size: 15, weight: .medium, design: .serif))
                .overlay(
                    RoundedRectangle(cornerRadius: 30)
                        .stroke(Color(red: 45 / 255, green: 0 / 255, blue: 112 / 255), lineWidth: 1)
                )
                .actionSheet(isPresented: $iliskiVisible, content: actionSheet)
                .onTapGesture {
                    self.iliskiVisible.toggle()
                }
        }
    }
    
    func actionSheet() -> ActionSheet {
        ActionSheet(
            title: Text("Bir ?li?ki Durumu Se?iniz"),
            message: Text("A?ag?da"),
            buttons: iliskiDurumlari.map { value in
                ActionSheet.Button.default(Text(value), action: {
                    self.iliski = value
                })
            }
        )
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
392 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you don't need user input (show keyboard) for this particular field, you can use Text instead:

var body: some View {
    VStack {
        Text(iliski) // <- change here
            .frame(width: 300, height: 50, alignment: .center)
            .padding(5)
            .font(Font.system(size: 15, weight: .medium, design: .serif))
            .overlay(
                RoundedRectangle(cornerRadius: 30)
                    .stroke(Color(red: 45 / 255, green: 0 / 255, blue: 112 / 255), lineWidth: 1)
            )
            .actionSheet(isPresented: $iliskiVisible, content: actionSheet)
            .onTapGesture {
                self.iliskiVisible.toggle()
            }
    }
}

and instead of placeholder add initial value:

@State private var iliski = "?li?ki Durumu Se?iniz.."

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...