简体   繁体   中英

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:

初始文本字段

With some 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())

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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