175

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 a UITextField, 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 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 textField 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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Vadoff
  • 9,219
  • 6
  • 43
  • 39
  • See [this answer](https://stackoverflow.com/a/19668174/168594). They suggest [UIResponder+KeyboardCache](https://github.com/mbrandonw/UIResponder-KeyboardCache). It's simple and awesome. Tested on iOS 7. – zekel Jan 16 '14 at 19:07
  • This is a known issue. Preloading keyboard seems promising. Check [Preloading the UIKeyboard.](http://web.archive.org/web/20120708055451/http://blog.weareuproar.com/preloading-the-uikeyboard) Some additional reading material: https://stackoverflow.com/questions/1628915/initial-iphone-virtual-keyboard-display-is-slow-for-a-uitextfield-is-this-hack https://stackoverflow.com/questions/8862659/uitextfield-keyboard-blocks-runloop-while-loading http://www.iphonedevsdk.com/forum/iphone-sdk-development/12114-uitextfield-loooong-delay-when-first-tapped.html – Rok Jarc Feb 20 '12 at 07:01
  • Thanks, 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?). – Vadoff Feb 20 '12 at 07:39
  • Did 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. – Rok Jarc Feb 20 '12 at 09:42
  • Yeah, 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. – Vadoff Feb 20 '12 at 10:17
  • Just 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. – Rok Jarc Feb 20 '12 at 10:28
  • Okay, 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). – Vadoff Feb 20 '12 at 22:36
  • 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? – Crashalot May 10 '16 at 22:36

7 Answers7

300

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.

Ash
  • 9,064
  • 3
  • 48
  • 59
149

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];
}
Vadoff
  • 9,219
  • 6
  • 43
  • 39
  • 1
    Nice and simple @Vadoff - this is an inline version of the UIResponder+KeyboardCache example given below – amergin Jan 17 '14 at 16:50
  • This 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. – n8tr Apr 10 '14 at 11:09
  • +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. – atomkirk May 04 '14 at 00:46
  • 4
    This should've been handled by the OS... but oh well : / – chakrit Jul 01 '14 at 19:48
  • Nice 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. – Matthew Ferguson Jul 29 '14 at 02:57
  • 9
    thanks! problem still remains on ios8 unfortunately, but this work-around still resolves it – hitme Oct 06 '14 at 03:59
  • Perfect, 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. – Jamie Brown Nov 05 '14 at 11:50
  • 1
    @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. – Dima Deplov Mar 06 '15 at 23:11
  • 16
    The fact that this is the correct answer and works makes me weep. I had to take a shower after applying this hack. – Bill Burgess Apr 01 '15 at 18:28
  • 1
    This is a bug? this still happen in iOS 9! [ >_< ] Apple knows about this problem? – kakashy Oct 31 '15 at 23:03
  • This solved the problem for me. Tested on Iphone 5s | IOS 9.0+ – Ritesh Kumar Gupta Dec 14 '15 at 04:12
  • @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! – Developer Apr 07 '16 at 08:48
  • This 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. – Bennett Smith May 17 '16 at 15:47
  • @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. – Shiv Feb 08 '19 at 01:37
27

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...

ABCD
  • 7,914
  • 9
  • 54
  • 90
  • 6
    It's due to the optimization level: ```Fastest, Smallest [-Os]```. You can change it on ```Build Settings > Optimization Level``` – Carlos Ricardo Jan 11 '13 at 11:34
18

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.

Gerrit Post
  • 1,247
  • 13
  • 25
Greg
  • 619
  • 1
  • 8
  • 15
5

Code in block added to main queue and run asynchronously. (don't locked main thread)

dispatch_async(dispatch_get_main_queue(), ^(void){
      [textField becomeFirstResponder];
 });
Sergey Petruk
  • 781
  • 7
  • 13
  • 6
    Try to add a textual explanation that describes why your code works and not just give it so that others may learn from it. – Sled Aug 22 '14 at 14:02
  • 7
    This code does in fact block the main thread, you're dispatching on the main queue... – Werner Altewischer Nov 11 '15 at 17:10
  • do you know difference between dispatch_async/dispatch_sync? And do you think [textField becomeFirstResponder]; is very difficult for main thread? – Sergey Petruk Nov 12 '15 at 09:39
  • @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. – Kevin Jan 06 '16 at 17:45
  • @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. – Sergey Petruk Jan 07 '16 at 19:12
  • @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...... – Kevin Jan 08 '16 at 08:00
  • Okay, 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) – Mike Gledhill Dec 21 '16 at 09:58
0

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.

Jeff Bowen
  • 5,904
  • 1
  • 28
  • 41
-2

You can add below code when viewController's view did loaded, like viewDidAppear.Not just application:didFinishLaunchingWithOptions:

UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
tianglin
  • 226
  • 3
  • 14