Super slow lag/delay on initial keyboard animation of UITextField
Solution 1
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];
}
Solution 2
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.
Solution 3
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...
Solution 4
This is a known issue.
Preloading keyboard seems promising. Check Preloading the UIKeyboard.
Some additional reading material:
Initial iPhone virtual keyboard display is slow for a UITextField. Is this hack around required?
UITextField keyboard blocks runloop while loading?
Solution 5
You can use Vadoff's solution in Swift by adding this to 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()
It is working for me in iOS 8.
Related videos on Youtube
Vadoff
Updated on July 08, 2022Comments
-
Vadoff almost 2 years
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.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 aUITextField
, and I still experience this problem. I'm using iOS 5, Xcode ver 4.2, and running on an 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
textField
become/resign first responder inviewDidAppear
, but that doesn't solve the problem entirely - it just loads the delay onto when the view loads instead. If I click ontextField
immediately 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. -
Vadoff about 12 yearsThanks, many of the links mention the delay to be about "1 second on older iphones", "not that noticeable on the 3g", and "loads instantly on new devices" but I'm experiencing a much longer delay of 3-4 seconds on an iphone 4s. I'll try preloading the keyboard next, but I'm worried that something else might be a problem (perhaps ios5 or my xcode ver?).
-
Rok Jarc about 12 yearsDid you try how UITextField & keyboard behave in a native app? Be sure to remove it from memory befor this test (double 'click' on home button...). There's nothing wrong with the piece of code you posted and 3-4 seconds really seem a lot - too much. I never tried this preloading but it looks like the only workaround.
-
Vadoff about 12 yearsYeah, the Search textField for Maps brings up the keyboard instantly when clicked after launch. I removed it from memory and tried a few times, it's instant every time. I'm not sure what's up with why mine's so slow.
-
Rok Jarc about 12 yearsJust did a test on one of my apps that use UITextField - the lag should really be minimal. The code you posted seems fine so there must be something else holding back the main thread. This might be a good time to launch the infamous Instruments.
-
Vadoff about 12 yearsOkay, for anyone else that seems to have this problem. I discovered it only happened on the first time the app was updated on the iphone from xcode. Once the app is loaded on the iphone, any further uses with the app will result in normal behavior (I removed the program from memory a few times/restarted my iphone to make sure).
-
Carlos Ricardo over 11 yearsIt's due to the optimization level:
Fastest, Smallest [-Os]
. You can change it onBuild Settings > Optimization Level
-
amergin over 10 yearsNice and simple @Vadoff - this is an inline version of the UIResponder+KeyboardCache example given below
-
n8tr about 10 yearsThis totally works for me on iOS 7.1. I actually used this solution and put it in a UIView subclass after it wakes from nib (my UIView subclass has a UITextField and the first time tapping was taking a few seconds on a iPhone 5). Now it's instant. I would recommend simply adding lagFreeField.hidden = YES; No need for the more complicated UIResponder+KeyboardCache solution as this totally works.
-
atomkirk about 10 years+1 totally works for me on iOS 7.1. I put this in the app delegate as described and the animation on the first keyboard appearance is smooth as butter.
-
chakrit almost 10 yearsThis should've been handled by the OS... but oh well : /
-
Matthew Ferguson almost 10 yearsNice work around. My client was nagging me for weeks. I agree with n8tr , just retain and hide the lagFreeField.hidden = YES; This will keep the keyboard loaded and no chance that the ARC will reference count and garbage collect.
-
Sled over 9 yearsTry to add a textual explanation that describes why your code works and not just give it so that others may learn from it.
-
hitme over 9 yearsthanks! problem still remains on ios8 unfortunately, but this work-around still resolves it
-
Jamie Brown over 9 yearsPerfect, and for reference, works fine with a Phonegap project too - textboxes in the UIWebView were doing exactly the same thing. Just add the code above to AppDelegate.m, at the bottom of didFinishLaunchingWithOptions.
-
Jonathan Beebe over 9 yearsThis was the correct answer in my case on an iPhone 5 running iOS 8.1 — I was experiencing about a 1 sec delay for the first keyboard.
-
Hassy over 9 yearsThanks nice solution :D
-
Dima Deplov about 9 years@Vadoff Solution works, but have 2 minuses: 1) I have about 1 extra second of app loading; 2) Received memory warning (but, not all the time). I tried to add this code to UIViewController in viewDidLoad, but had no effect, maybe I made something wrong? Could you edit your answer and add code for view controller, if this possible.
-
Bill Burgess about 9 yearsThe fact that this is the correct answer and works makes me weep. I had to take a shower after applying this hack.
-
Jasper almost 9 yearsGood find. However on a previous project I had to use the 'exotic hack' solution
-
Sound Blaster almost 9 yearsThank you! iOS 8.4 iPhone 5C - works fine not under debug!
-
Pauls over 8 yearsYep, the lagg occurs only when debugging. Weird.
-
Jim75 over 8 yearsgreat. debugging mode was the problem here too.
-
Mohamed Emad Hegab over 8 yearsI had my doubts about this.. cause all old apps on appstore works fine .. so it must be xcode problem.. thanks :)
-
RJH over 8 yearsthis was driving me nuts. thanks for the tip - its spot on
-
spassas over 8 yearsIt seems that on iOS 9.0.1 this issue has been fixed
-
Ash over 8 yearsIt still happens for me when using Objective C, so may only be fixed in Swift 2.0
-
Yaroslav over 8 yearsThank you. This issue is still present on iOS 9.1/Xcode 7.1, Swift 2.1.
-
kakashy over 8 yearsThis is a bug? this still happen in iOS 9! [ >_< ] Apple knows about this problem?
-
Werner Altewischer over 8 yearsThis code does in fact block the main thread, you're dispatching on the main queue...
-
Sergey Petruk over 8 yearsdo you know difference between dispatch_async/dispatch_sync? And do you think [textField becomeFirstResponder]; is very difficult for main thread?
-
Ritesh Kumar Gupta over 8 yearsThis solved the problem for me. Tested on Iphone 5s | IOS 9.0+
-
BaseZen over 8 yearsFYVM Apple, FYVM. 90 minutes of time on a deadline I can't afford. 20 upvotes if I could. Still present in iOS9.2/Xcode 7.2
-
Kevin over 8 years@Spetruk The thread calling dispatch_async isn't blocked, but the thread that you actually run the code on is definitely blocked. A single thread can't do two things at once, so this code blocks the main thread (because of dispatch_get_main_queue) but doesn't block the thread where dispatch_async is called.
-
Sergey Petruk over 8 years@Kevin but something is blocking main thread and as result keyboard animation is't working. I agree with you, but my opinion, solution with textFiled in appDelegate smells badly to.
-
Kevin over 8 years@Spetruk Yes, it all sucks. The only clean way is for Apple to just fix their framework, and I trust they will, at some point......
-
Alex Zavatone over 8 yearsI have this EXACT same problem when my app starts up from Xcode, but only on an iPad. Xcode 7.1. iOS 9.1. iPad Air 2. Same fix as well.
-
Deprecated Darren over 8 yearsThis was exactly what my issue was too. After I unplugged from my mac there was no delay (ios9) xcode 7.2
-
Kernelzero about 8 yearsI solve it when reboot device. iOS9.2, xcode 7.3 iPhone5s
-
Developer about 8 years@Vadoff I am sorry but I don't like these kind of hacks as there should be some genuine reason of this issue and thus there should be genuine solution to this issue. I found Ash answer working fine for me. I know it will hamper the Debugging. So your answer should be applicable in case of debugging only Not in a general way!
-
Nicolas Miari about 8 yearsStill happening on iOS 9.3.1, Xcode 7.3, Swift 2.x (Only with debugger attached).
-
Trevor Panhorst about 8 yearsThis is the solution for me as well. Preloading the keyboard sort of solves this while debugging. It simply causes the app to take longer while loading...
-
Bennett Smith about 8 yearsThis should not be marked as the correct answer. This solution is only appropriate for correcting a delay in showing the keyboard while in the debugger. Please check the answer provided by @Ash. I have yet to see a release build of an app that exhibits this slow initial keyboard loading issue.
-
Jolly Roger almost 8 yearsHoly crap, upvote for you, sir! Just to make it even worse, if you put the becomeFirstResponder() inside viewWillAppear() the whole VC is stalled from appearing and you get an unresponsive blank screen instead of just a slow keyboard. Typically 10 seconds lag between viewWillAppear and viewDidAppear!
-
lppier almost 8 yearsIndeed... this is weird. Thanks for saving me some time.
-
Adarkas2302 over 7 yearsit seems to be fixed on the iPhone but i still have the lag on the iPad.
-
Mike Gledhill over 7 yearsThis worked... but this problem is so dumb. How do Apple manage to make each iOS release even more frustrating than the last ?!
-
Mike Gledhill over 7 yearsOkay, this might not block the main thread... but it still takes 4-5 seconds for the keyboard to appear (using iOS 9.3.5, in December 2016)
-
Shiv over 5 years@BennettSmith I have plenty of apps installed that have absolutely abysmal first keyboard load performance. Definitely not attached to debugger. Would love to know if this solves the issue with those but that certainly seems like an OS issue.
-
John Leonardo almost 5 yearsThis answer still holds true in 2019, thanks for saving me time.
-
David about 4 yearsStill true in April 2020 with a default UITextField. Happens only while debugging.