UITextField no longer reloads keyboardType after reloadInputViews call

笑着哭i 提交于 2019-12-14 04:16:24

问题


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:@""];


来源:https://stackoverflow.com/questions/24546339/uitextfield-no-longer-reloads-keyboardtype-after-reloadinputviews-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!