UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Increasing Textfield padding does not increase tappable area

Forums > SwiftUI

I just don't understand how something so simple is still so difficult to pull off in SwiftUI's current implementation. All I want is to create a textfield with an increased height and rounded corners. That part works. However tapping on the padding does not bring the textfield into focus. Also the padding is not respected in regards to the default keyboard avoidance in a scrollview. Do I really need to switch back to UIKit to pull this off? I just can't believe this hasn't been properly resolved at this point.

I've searched and I've searched and there isn't one decent solution to this problem. Some have people have come up with solutions to tappable area problem but the keyboard avoidance problem still persists. Has anyone been able to pull this off properly without resorting to UIKit??? I mean there no real way to increase the Textfield frame?? I just can't believe it.

1      

Yep, I should have added some code. Sorry about that. Here's a sample to illustrate the issue. If you tap on the outside padding, you won't get focus. Also if you tap on the inside and do get focus, the keyboard should clip the blue padding as it doesn't take it into account when calculating the textfield height. Like I said, I haven't found any solution align that accounts for increasing the tappable area AND nicely handles keyboard avoidance. I'm currently switch to UIKit for my textfields until I do.

  ScrollView {
      VStack {
        Spacer()
          .frame(height: 600)

        TextField("", text: .constant(""))
          .padding(30)
          .background(RoundedRectangle(cornerRadius: 5).fill(.blue))
          .overlay(
            RoundedRectangle(cornerRadius: 4)
              .stroke(lineWidth: 1)
              .foregroundColor(.gray)
          )
          .foregroundColor(.black)
      }
    }

1      

Hi @SuperGood1975,

I came across your thread today while searching for something else. The answer to your question is to use .contentShape() to make the empty space tappable. Append the following to your TextField and it should work as expected:

  .focused($focusedField, equals: .yourTextField)
  .contentShape(RoundedRectangle(cornerRadius: 5))
  .onTapGesture { focusedField = .yourTextField }

This solution also assumes you add the following to your code:

  @FocusedState private var focusedField: Field?

  private enum Field: Int, Hashable {
    case yourTextField, yourOtherTextField
  }

However, while the .contentShape / .onTapGesture combo is perhaps easiest to code, you can instead wrap the field in a button and achieve the same result with better accessibility.

  Button { 
    focusedField = .yourTextField
  } label: {            
    TextField("", text: .constant(""))
      ...
      .focused($focusedField, equals: .yourTextField)
   }

Hope this helps.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.