UITextField的初始键盘animation超级慢速滞后/延迟

好的,这个问题一直让我疯狂。

触摸我的UITextField后,键盘popup大概需要3-4秒。 这只会在应用程序启动后第一次popup键盘,然后立即开始animation。

起初我以为这是加载太多的图像或我的UITableView ,但我只创build了一个全新的项目只有一个UITextField ,我仍然遇到这个问题。 我使用iOS 5,Xcode ver 4.2,并在iPhone 4S上运行。

这是我的代码:

 #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 

这是所有应用程序的常见问题?

现在,我可以做得更好的唯一方法是让textFieldviewDidAppear成为/ resign第一个响应者,但是这并不能完全解决问题 – 它只是在视图加载的时候加载延迟。 如果我加载视图时立即点击textField ,我仍然得到这个问题; 如果我在触摸textField之前等待3-4秒,我没有得到延迟。

在实施任何异乎寻常的黑客来解决此问题之前,请尝试:停止debugging会话,从多任务closures应用程序,从计算机上拔下您的设备,并通过点击其图标正常运行应用程序。 至less有两种情况只是在设备插入时才会出现延迟。

所以这个问题不仅仅局限于我以前想的第一次安装,而是每次应用程序启动时都会发生。 这是我的解决scheme,完全解决了这个问题。

 - (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]; } 

是的,我也在最新的iPhone 4s上延迟了几秒钟。 不要惊慌。 由于某些原因,只会在第一次在Debug中从Xcode加载应用程序。 当我释放时,我没有得到延迟。 把它忘了吧…

这是一个已知的问题。

预加载键盘似乎很有前途。 检查预加载UIKeyboard。

一些额外的阅读材料:

对于UITextField,初始iPhone虚拟键盘显示速度较慢。 这是围绕需要吗?

UITextField键盘在加载时阻塞runloop?

http://www.iphonedevsdk.com/forum/iphone-sdk-development/12114-uitextfield-loooong-delay-when-first-tapped.html

您可以在Swift中使用Vadoff的解决scheme,将其添加到didFinishLaunchingWithOptions中:

 // 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() 

它在iOS 8中为我工作。

代码块添加到主队列中并asynchronous运行。 (不locking主线程)

 dispatch_async(dispatch_get_main_queue(), ^(void){ [textField becomeFirstResponder]; }); 

看到这个答案 。 他们build议使用UIResponder + KeyboardCache 。 这很简单,很棒。 在iOS 7上testing

一个相关的问题,一个UIViewController将缓慢呈现,通过使用系统字体而不是UITextField上的自定义字体来解决。 也许使用系统字体也​​可以解决这个问题?

这个bug似乎在iOS 9.2.1中得到解决。 由于升级我的设备,我不再有点击文本字段和当我的设备连接到我的电脑时出现的键盘之间的延迟。

这个选定的答案导致iOS 11上的BAD_EXC崩溃 – 从应用程序中删除以修复

当viewController的视图没有加载时,你可以添加下面的代码,比如viewDidAppear.Not只是应用程序:didFinishLaunchingWithOptions:

 UITextField *lagFreeField = [[UITextField alloc] init]; [self.window addSubview:lagFreeField]; [lagFreeField becomeFirstResponder]; [lagFreeField resignFirstResponder]; [lagFreeField removeFromSuperview];