Software⏱️ 2 min read📅 2026-06-03

How to Fix: How can I make a UITextField move up when the keyboard is present - on starting to edit?

iOS SDK solution for scrolling UITextField with keyboard present.

Quick Answer: Use a UIScrollView and add a constraint to the UITextField to move up when the keyboard appears, while also setting the scroll view's content size to match the text field's height.

To solve this problem, you need to use a UIScrollView in conjunction with your UIView. Here's how:

🔍 Implementing the Solution

  • [Step 1: Add a UIScrollView to your UIView](#step-1-add-a-scrollview-to-your-view)

Step 1: Add a UIScrollView to your UIView

Add a UIScrollView to your view by creating an instance of it and setting its delegate property to your view. You also need to set the scrollViewDelegate property of your view to the same delegate.

let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 300, height: 200)) scrollView.delegate = self self.scrollView.scrollViewDelegate = self
  • [Step 2: Configure the UIScrollView](#step-2-configure-the-scrollview)

Step 2: Configure the UIScrollView

Configure your UIScrollView by setting its contentSize property to match the size of your view, and set its scrollIndicatorStyle property to UIScrollIndicatorHorizontal. You also need to add a UITextInputTraits instance to your scroll view's content view.

scrollView.contentSize = CGSize(width: 300, height: 200) scrollView.scrollIndicatorStyle = .horizontal let contentView = scrollView.contentView as! UIView contentView.addSubview(self.textField) contentView.addConstraints([NSLayoutConstraint.constraints(with: contentView, item: self.textField, attribute: .top, relatedBy: .atMost, toItem: contentView, attribute: .bottom, multiplier: 1, constant: -100)])

🎯 Final Words

By following these steps, you should be able to create a UIScrollView that scrolls your UITextFields up when the keyboard is present.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions