Programmatically change focus after input text in UITextfield

I have a simple view which has two textviews as well as a button. All controls are positioned vertically. I can navigate from one control to another by using the remote. However, I'm trying to get the following scenario working:

  1. The user launches the app (focus is on first textfield)
  2. The user enters the textfield and uses the fullscreen keyboard, then she hits the "Next" button
  3. The app shows the second textfield (fullscreen keyboard), types in some text and hits the "Done" button
  4. The app leaves the fullscreen keyboard and focuses the button

While step 1-3 is working out of the box, changing the focus programatically (4) doesn't work. Here's some code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == self.textfieldB) {
        self.activeView = self.textfieldB;
        [self setNeedsFocusUpdate];
        [self updateFocusIfNeeded];
        return YES;
    return NO;

and in - (UIView *)preferredFocusedView I'm returning whatever is stored in self.activeView (yes, it will return the button instance). All methods are called as expected, however the focus doesn't change. What am I doing wrong here?

Thanks in advance!

According to this developer forums thread the behaviour is intentional:

When dismissing a presented view controller, like a fullscreen keyboard, UIKit will automatically return focus back to where it was when that view controller was first presented, if that view is still focusable. This is so that focus automatically returns back to where the user would expect it to be. There is currently no mechanism for overriding this behavior.

In a similar case I switched the focus with a delay after returning from the keyboard screen. It’s an ugly hack, I wish UIKit wasn’t so opinionated about this.

UITextField, This property specifies the type of keyboard displayed when the text field is active . You can access the value of this attribute programmatically using the text field's � Right-click and drag from the UITextField to your ViewController.swift code file; Change the dropdown from Outlet to Action. Choose Editing Changed; Type a method name: textFieldEditingDidChange; 2 - Connect Outlets for the text field. Drag over connections for the UITextField, and you can use the textField.text property to get it’s value.

It can be changed if the containing UIViewController has restoresFocusAfterTransition set to false

Just set restoresFocusAfterTransition = false and then override preferredFocusEnvironments

Custom UITextField appearance?, However, when I actually enter the text fields (show keyboard, enter something Even programmatically changing the text color after focus leaves the text field� The simplified sample I wrote contains of three input text fields and three command buttons. Initially, using the af:document InitialFocusId property, the cursor focus is set to the first input text field. The command button send a partial submit to a managed bean on the server.

Yes, you can achieve step 4 by doing this:

    @IBOutlet weak var loginButton: UIButton!
    var focusedView: UIView?

    override var preferredFocusedView: UIView? {
        return focusedView

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == passwordTextField {
            focusedView = loginButton
        return true

How to Get UITextField Text Editing Changed Events for User Input , Do you need to get user input from your UITextField change events as your user types in a text field? Are you trying to validate text to hide or show a status message? Programmatically Add Selectors for UITextFieldChanged Events the parameters using (_:) since the label is now _ in Swift 4) textField. If you’re desperately opposed to using tags, the other solution is to place your labels in an array, find the position of the text field that triggered the event, then move one down in the array. Note: If you ever need to force the first responder to resign itself and aren’t sure which text field is in control, it’s easier to use view

SwiftUI programmatically focus TextField? : swift, I'd like to autofocus a TextField in a modal when it's presented. etc. there's extreme amounts of stuff that's going to change before it's “fully” ready UITextField and UIViewRepresentable in order to gain smart focus feature. Android - How to change ActionBar background color programmatically ; Android dp to pixel conversion ; Android get screen width and height programmatically ; Android - How to make TextView clickable programmatically ; Android - How to set and get TextView Background Color Programmatically ; Android TextView set html text

how to select but not focus textfield swift Code Example, Get code examples like "how to select but not focus textfield swift" instantly right func textFieldShouldBeginEditing(textField: UITextField) -> Bool { Expression of type 'UIViewController?' is unused warning when pop view to change the font of buttons programmatically swift � how to change the text of a� In most cases, instances of the UITextField or UITextView classes send a sequence of similarly named messages to their delegates when there is a change (or impending change) in first-responder status for a given text object. When the user taps a text object, it automatically becomes first responder; as a result, the system displays the keyboard

How to move to the next UITextField when the user presses return , How to move to the next UITextField when the user presses return There is no built-in way of making this happen, so we need to write code ourselves using The easiest approach is using view tags: give your text fields� Returns the input view that provides the coordinate system for geometric operations within the text input. GetTextStyling(IUITextInput, UITextPosition, UITextStorageDirection) Returns a dictionary of style properties for text at the atPosition position. InsertDictationResult(IUITextInput, NSArray) Inserts a dictation result at the current position.

  • I am also facing the same issue. Thanks for the answer