1

I have the following problem: I have a Form Sheet presented modally with a UITextField on it. When tapping the UITextField, it takes about 3 secs until the Keyboard will show up, which is very slow. Does anybody have an idea what the problem could be?

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
  if (textField == self.licenseTextField) {
    [self.licenseTextField resignFirstResponder];
  } return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.licenseTextField resignFirstResponder]; }

-(BOOL)disablesAutomaticKeyboardDismissal{ return NO; } 
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Before applying any hack in source code, please test your app without debugging mode and without plugged in. A lot of iOS version has this issue. When you test your app in real environment there will be no loading. Thanks – KD_rock Jul 01 '16 at 06:59

1 Answers1

1

iOS does not free memory for the keyboard unless it has to show the keyboard. There are some workarounds, that are not exactly fancy but functional.

This is a solution that I use. I preload the keyboard on app start before showing the actual content of my app. The starting process takes a little longer but at least my interface does not freeze when I show the keyboard later on.

// Update: Apple just rejected an app of mine using the method shown below for the second time, as it launches to a black screen on the iPad simulator (not the device!) sometimes, god knows why.

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

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

    return YES;
}

- (void)keyboardDidShow:(NSNotification *) notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        }

        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        [self.window makeKeyAndVisible];
    });
}

The dispatch fixed some animation issues I had, maybe you don't need it. More information on the problem and possible solutions.

Community
  • 1
  • 1
Johannes
  • 325
  • 4
  • 11
  • Thanks a lot! This has solved it, even though the startup process takes a little longer. I still hope for a solution without workaround. – konfusius1991 Jun 16 '14 at 13:04