UITextField no longer reloads keyboardType after r

2019-04-05 14:49发布

问题:

In iOS 7, I could change the keyboard type while it is the firstResponder (on the fly):

if (textField.text.length > 2) {

    textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
else
{
    textField.keyboardType = UIKeyboardTypeDefault;
}

[textField reloadInputViews];

// (Omitting some efficiency stuff to keep example to bare bones)

This no longer works under Xcode 6/iOS 8. The documentations mostly reflect changes regarding custom keyboard.

Using resign/become first responder is (still) working:

[textField resignFirstResponder];

// Make keyboard change

[textField becomeFirstResponder];

But it just feels like an overkill. It's tearing and rebuilding a wall, just to change a picture on it.

There is a related post here: UITextView does not seem to implement reloadInputViews

But it seems that the solution (in a comment) is "apparently declaring it as a UITextView instead of a UIResponder affects how it behaves during runtime. ... and it works now"

In my case it is a UITextField, and I tried to cast to UITextView just in case. No go.

I'll mention again that it is working well under iOS7 / Xcode5.

I don't really know if this is a 'beta' issue with Xcode 6, or a design change in iOS 8.

回答1:

I found the same issue. It is better to check whether the textField is already the firstResponder or not.

[textField reloadInputViews]; // does not work on iOS8 !

if ([textField isFirstResponder]) {
    [textField resignFirstResponder];
    [textField becomeFirstResponder];
}

Not a clean way though, but it works.



回答2:

I found that this works when the textfield is first responder:

[self.textField reloadInputViews];
[self.textField setText:@" "];
[self.textField setText:@""];


标签: ios8 xcode6