Textfield with too small hit area

Hello,

It's impossible to manage hit area for textfield and it's already too small by default (detected with Accessibility Inspector).

I'm tried to force with modifier "frame(minHeight: xxx)", the component change the height but the hit area is still in same size.

Have to tips to fix this issue?

Answered by DTS Engineer in 855613022

Hello,

Long delay here, but this came up for me recently, figured I'd post the solution on the forums for all to benefit :)

The following approach will increase the hit-area of a view without the side-effects on layout:

TextField("Example", text: $text)
    .focused($isFocused)
    .contentShape(Rectangle().inset(by: -20)) // The negative inset on the content shape is key. This expands the content shape without affecting layout.
    .onTapGesture {
        isFocused = true
    }

Hope this helps others dealing with a similar issue!

--Greg

There doesn't seem to be any direct solution without UIViewRepresentable (which means turning around SwiftUI…).

https://stackoverflow.com/questions/56795712/swiftui-textfield-touchable-area

Hello,

Long delay here, but this came up for me recently, figured I'd post the solution on the forums for all to benefit :)

The following approach will increase the hit-area of a view without the side-effects on layout:

TextField("Example", text: $text)
    .focused($isFocused)
    .contentShape(Rectangle().inset(by: -20)) // The negative inset on the content shape is key. This expands the content shape without affecting layout.
    .onTapGesture {
        isFocused = true
    }

Hope this helps others dealing with a similar issue!

--Greg

Textfield with too small hit area
 
 
Q