Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

ios - SwiftUI - Prevent TextField from expanding when text is entered

I have a TextField with a fixed-size frame, but it still expands to wrap the entered text, even over siblings.

Initial state:

Initial TextField

With some input:

TextField with input

Is there a way to prevent this in SwiftUI using TextField or do I need to resort to ViewRepresentable?

My code for this layout looks something like:

HStack(spacing: 0) {
    Text("1").fixedSize(horizontal: true, vertical: false).frame(width: 22)
    TextField("Price", text: $text1).fixedSize(horizontal: true, vertical: false).frame(width: 70)
    TextField("1", text: $text2).fixedSize(horizontal: true, vertical: false).frame(width: 30)
    TextField("1", text: $text3).fixedSize(horizontal: true, vertical: false).frame(width: 70)
}.textFieldStyle(RoundedBorderTextFieldStyle())

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

1 Answer

0 votes
by (71.8m points)

Change order of modifiers, like

TextField("1", text: $text3)
   .frame(width: 70)                                // << here !!
   .fixedSize(horizontal: true, vertical: false)

Tested with Xcode 12.1 / iOS 14.1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...