Super slow lag/delay on initial keyboard animation of UITextField

40,522

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?

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

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.

Share:
40,522

Related videos on Youtube

Vadoff
Author by

Vadoff

Updated on July 08, 2022

Comments

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

  • Vadoff
    Vadoff about 12 years
    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?).
  • Rok Jarc
    Rok Jarc about 12 years
    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.
  • Vadoff
    Vadoff about 12 years
    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.
  • Rok Jarc
    Rok Jarc about 12 years
    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.
  • Vadoff
    Vadoff about 12 years
    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).
  • Carlos Ricardo
    Carlos Ricardo over 11 years
    It's due to the optimization level: Fastest, Smallest [-Os]. You can change it on Build Settings > Optimization Level
  • amergin
    amergin over 10 years
    Nice and simple @Vadoff - this is an inline version of the UIResponder+KeyboardCache example given below
  • n8tr
    n8tr about 10 years
    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.
  • atomkirk
    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
    chakrit almost 10 years
    This should've been handled by the OS... but oh well : /
  • Matthew Ferguson
    Matthew Ferguson almost 10 years
    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.
  • Sled
    Sled over 9 years
    Try to add a textual explanation that describes why your code works and not just give it so that others may learn from it.
  • hitme
    hitme over 9 years
    thanks! problem still remains on ios8 unfortunately, but this work-around still resolves it
  • Jamie Brown
    Jamie Brown over 9 years
    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.
  • Jonathan Beebe
    Jonathan Beebe over 9 years
    This 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
    Hassy over 9 years
    Thanks nice solution :D
  • Dima Deplov
    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
    Bill Burgess about 9 years
    The fact that this is the correct answer and works makes me weep. I had to take a shower after applying this hack.
  • Jasper
    Jasper almost 9 years
    Good find. However on a previous project I had to use the 'exotic hack' solution
  • Sound Blaster
    Sound Blaster almost 9 years
    Thank you! iOS 8.4 iPhone 5C - works fine not under debug!
  • Pauls
    Pauls over 8 years
    Yep, the lagg occurs only when debugging. Weird.
  • Jim75
    Jim75 over 8 years
    great. debugging mode was the problem here too.
  • Mohamed Emad Hegab
    Mohamed Emad Hegab over 8 years
    I had my doubts about this.. cause all old apps on appstore works fine .. so it must be xcode problem.. thanks :)
  • RJH
    RJH over 8 years
    this was driving me nuts. thanks for the tip - its spot on
  • spassas
    spassas over 8 years
    It seems that on iOS 9.0.1 this issue has been fixed
  • Ash
    Ash over 8 years
    It still happens for me when using Objective C, so may only be fixed in Swift 2.0
  • Yaroslav
    Yaroslav over 8 years
    Thank you. This issue is still present on iOS 9.1/Xcode 7.1, Swift 2.1.
  • kakashy
    kakashy over 8 years
    This is a bug? this still happen in iOS 9! [ >_< ] Apple knows about this problem?
  • Werner Altewischer
    Werner Altewischer over 8 years
    This code does in fact block the main thread, you're dispatching on the main queue...
  • Sergey Petruk
    Sergey Petruk over 8 years
    do you know difference between dispatch_async/dispatch_sync? And do you think [textField becomeFirstResponder]; is very difficult for main thread?
  • Ritesh Kumar Gupta
    Ritesh Kumar Gupta over 8 years
    This solved the problem for me. Tested on Iphone 5s | IOS 9.0+
  • BaseZen
    BaseZen over 8 years
    FYVM 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
    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
    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
    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
    Alex Zavatone over 8 years
    I 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
    Deprecated Darren over 8 years
    This was exactly what my issue was too. After I unplugged from my mac there was no delay (ios9) xcode 7.2
  • Kernelzero
    Kernelzero about 8 years
    I solve it when reboot device. iOS9.2, xcode 7.3 iPhone5s
  • Developer
    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
    Nicolas Miari about 8 years
    Still happening on iOS 9.3.1, Xcode 7.3, Swift 2.x (Only with debugger attached).
  • Trevor Panhorst
    Trevor Panhorst about 8 years
    This 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
    Bennett Smith about 8 years
    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.
  • Jolly Roger
    Jolly Roger almost 8 years
    Holy 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
    lppier almost 8 years
    Indeed... this is weird. Thanks for saving me some time.
  • Adarkas2302
    Adarkas2302 over 7 years
    it seems to be fixed on the iPhone but i still have the lag on the iPad.
  • Mike Gledhill
    Mike Gledhill over 7 years
    This worked... but this problem is so dumb. How do Apple manage to make each iOS release even more frustrating than the last ?!
  • Mike Gledhill
    Mike Gledhill over 7 years
    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)
  • Shiv
    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
    John Leonardo almost 5 years
    This answer still holds true in 2019, thanks for saving me time.
  • David
    David about 4 years
    Still true in April 2020 with a default UITextField. Happens only while debugging.