ios UITextField 初始键盘动画的超慢滞后/延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9357026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Super slow lag/delay on initial keyboard animation of UITextField
提问by Vadoff
Alright, this problem has been driving me nuts.
好吧,这个问题一直让我发疯。
It takes roughly 3-4 seconds for the keyboard to pop up after I touch my UITextField. This only occurs on the first time the keyboard pops up since the app launched, afterwards the animation starts instantly.
触摸我的UITextField. 这只发生在自应用程序启动后键盘第一次弹出时,然后动画立即开始。
At first I thought it was problem of loading too many images, or my UITableView, but I just created a brand new project with only a UITextField, and I still experience this problem. I'm using iOS 5, Xcode ver 4.2, and running on an iPhone 4S.
一开始我以为是图片加载太多的问题,或者我的UITableView,但是我刚刚创建了一个只有一个的全新项目UITextField,我仍然遇到这个问题。我使用的是 iOS 5、Xcode 4.2 版,并在 iPhone 4S 上运行。
This is my code:
这是我的代码:
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.delegate = self;
[self.view addSubview:textField];
}
@end
Is this a common problem for all apps?
这是所有应用程序的常见问题吗?
Right now, the only way I can make it somewhat better is by having textFieldbecome/resign first responder in viewDidAppear, but that doesn't solve the problem entirely - it just loads the delay onto when the view loads instead. If I click on textFieldimmediately when the view loads, I still get the problem; if I wait 3-4 seconds after the view loads before touching the textField, I don't get the delay.
现在,我可以让它变得更好的唯一方法是textField成为/退出第一响应者viewDidAppear,但这并不能完全解决问题 - 它只是在视图加载时加载延迟。如果我textField在视图加载时立即单击,我仍然会遇到问题;如果我在视图加载后等待 3-4 秒再触摸 textField,则不会出现延迟。
采纳答案by Vadoff
So the problem is NOT just limited to the first install as I had previously thought, but happens every time the app is launched. Here's my solution that solves the issue completely.
因此,问题不仅限于我之前认为的第一次安装,而是每次启动应用程序时都会发生。这是我完全解决问题的解决方案。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Preloads keyboard so there's no lag on initial keyboard appearance.
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
}
回答by Ash
Before you implement any exotic hacks to get around this problem, try this: stop the debug session, close the app from multitasking, unplug your device from the computer and run the app normally by tapping its icon. I have seen at least two cases in which the delay only occurs while the device is plugged in.
在您实施任何奇特的黑客来解决此问题之前,请尝试以下操作:停止调试会话,关闭多任务处理应用程序,从计算机上拔下设备并通过点击其图标正常运行应用程序。我至少见过两种情况,其中延迟仅在插入设备时发生。
回答by HelloWorld
Yeah, I also got a few seconds delay on the latest iPhone 4s. Don't panic. For some reasons, it only happens the first time the app is loaded from Xcode in Debug. When I did Release, I don't get the delay. Just forget it...
是的,我在最新的 iPhone 4s 上也有几秒钟的延迟。不要惊慌。出于某些原因,它仅在第一次从 Xcode 中的 Debug 加载应用程序时发生。当我释放时,我没有得到延迟。把它忘了吧...
回答by Rok Jarc
This is a known issue.
这是一个已知的问题。
Preloading keyboard seems promising. Check Preloading the UIKeyboard.
预装键盘似乎很有希望。检查预加载 UIKeyboard。
Some additional reading material:
一些额外的阅读材料:
Initial iPhone virtual keyboard display is slow for a UITextField. Is this hack around required?
UITextField 的初始 iPhone 虚拟键盘显示速度很慢。需要这种黑客攻击吗?
UITextField keyboard blocks runloop while loading?
回答by Greg
You can use Vadoff's solution in Swift by adding this to didFinishLaunchingWithOptions:
您可以通过将其添加到 didFinishLaunchingWithOptions 来在 Swift 中使用 Vadoff 的解决方案:
// Preloads keyboard so there's no lag on initial keyboard appearance.
let lagFreeField: UITextField = UITextField()
self.window?.addSubview(lagFreeField)
lagFreeField.becomeFirstResponder()
lagFreeField.resignFirstResponder()
lagFreeField.removeFromSuperview()
It is working for me in iOS 8.
它在 iOS 8 中对我有用。
回答by Sergey Petruk
Code in block added to main queue and run asynchronously. (don't locked main thread)
块中的代码添加到主队列并异步运行。(不要锁定主线程)
dispatch_async(dispatch_get_main_queue(), ^(void){
[textField becomeFirstResponder];
});
回答by zekel
See this answer. They suggest UIResponder+KeyboardCache. It's simple and awesome. Tested on iOS 7.
看到这个答案。他们建议UIResponder+KeyboardCache。这很简单而且很棒。在 iOS 7 上测试。
回答by Crashalot
A related problem, where a UIViewController would be slow to present, was solved by using the system font instead of a custom font on the UITextField. Perhaps using the system font might also work for this problem?
通过在 UITextField 上使用系统字体而不是自定义字体解决了 UIViewController 显示缓慢的相关问题。也许使用系统字体也可以解决这个问题?
回答by Manesh
This selected answer causes BAD_EXC crash on iOS 11 - remove from app to fix
此选择的答案会导致 iOS 11 上的 BAD_EXC 崩溃 - 从应用程序中删除以修复
回答by Jeff Bowen
This bug seems to be fixed in iOS 9.2.1. Since upgrading my device, I no longer have a delay between tapping a text field and the keyboard appearing when my device is connected to my computer.
此错误似乎已在 iOS 9.2.1 中修复。自从升级我的设备后,当我的设备连接到我的计算机时,我在点击文本字段和键盘之间不再有延迟。

