1

I am currently writing an iOS app and encountered some animation issue. The following is the test code to show my question. It's just a very simple animation that lets the textField to move up when the keyboard shows and move down when the keyboard hides. Interestingly, if it is an English keyboard, it seems that the UIView animation somehow conflicts with the keyboard animation and no animations are performed, but if I switch to the Chinese keyboard, it works fine. I just want to confirm that whether it is a bug from apple....

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITextField *textField = [[UITextField alloc] init];
    self.textField = textField;
    textField.placeholder = @"Click me!!";
    textField.frame = CGRectMake(100, 350, 150, 40);
    [self.view addSubview:self.textField];
    self.textField.delegate = self;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.textField.frame;
        frame.origin.y -= 100;
        self.textField.frame = frame;
    }];
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField resignFirstResponder];
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.textField.frame;
        frame.origin.y += 100;
        self.textField.frame = frame;
    }];
    return YES;
}

UPDATE

It seems that it is a bug from apple... I tried to use iOS 8.3 simulator, and it works super smoothly... In iOS 9.3, if you just try showing and hiding the keyboard, even without moving the textField, the animation will get stuck after several operations...

Lin Fu
  • 11
  • 3
  • i will try this code but not any problem.try to take textfield in storyboard than use – Jigar Jun 28 '16 at 03:33
  • What xcode and ios versions do you use? – Lin Fu Jun 28 '16 at 03:42
  • i will use xcode 7.3 and ios 9.3 – Jigar Jun 28 '16 at 03:48
  • remove viewDidLoad method code and take textfield in storyboard – Jigar Jun 28 '16 at 03:48
  • I've tried.. nothing changes.. And I've also tried not to use the delegate method but use the NSNotificationcenter, it's the same... If you got some different results, please let me know, thanks! – Lin Fu Jun 28 '16 at 04:09
  • Also faced with this problem on iOS 9 and 10. Sometimes it stucks on device and on simulator. But everything works good when I enable slow animations mode on simulator. Weird... – Serhiy Dec 15 '16 at 08:56

3 Answers3

2

I'd advise you to move the animation blocks to methods from the notification UIKeyboardWill{Show|Hide}Notification. The delegate methods from your text field are asking about the text field and its behavior – nothing to do with the keyboard itself (and you can even synchronize the duration, curve and delay).

You're returning YES to -textFieldShouldReturn: which is only supposed to check if the text field should process the return key tap.

From my experience with animations, sometimes everything works fine when testing with a real device. I made the suggested changes over here and everything worked fine on an iPhone 5c (iOS 8.1) and iPhone 6s Plus (iOS 10, Beta 1).


Here's a snippet of how it looks like:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNotifications];
    [self initUIElements];
}

- (void)initUIElements {
    CGRect frame = CGRectMake(0.f, 20.f, 200.f, 44.f);
    self.textField = [[UITextField alloc] initWithFrame:frame];
    self.textField.center = CGPointMake(self.view.center.x, 32.f);
    self.textField.placeholder = @"Placeholder";
    self.textField.delegate = self;
    [self.view addSubview:self.textField];

    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50.f, 50.f)];
    self.imageView.center = self.view.center;
    self.imageView.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:self.imageView];
}

- (void)setupNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)keyboardWillHide:(id)sender {
    [UIView animateWithDuration:1.f animations:^{
        self.textField.center = CGPointMake(self.view.center.x, 32.f);
        self.imageView.center = self.view.center;
    }];
}

- (void)keyboardWillShow:(id)sender {
    [UIView animateWithDuration:1.f animations:^{
        self.textField.center = CGPointMake(self.view.center.x, 62.f);
        self.imageView.center = CGPointMake(self.view.center.x, (self.view.center.y - 50.f));
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
Community
  • 1
  • 1
Alberto S.
  • 81
  • 6
  • Thanks very much to your answer, it works better on the real device but not always better... It seems that it is a bug only for iOS9.3, which you haven't tested on... I've tested on iOS 9.1 and iOS 8.3, both work extremely well... BTW, I don't think it has anything to do with the delegate method, I've also tried the notificationcenter, and it fails in the same way (again, in the simulator with iOS 9.3). – Lin Fu Jun 28 '16 at 16:34
  • @LinFu Sure! I'll try finding a way to test on 9.3 and will let you know. Even though changing to the notification methods doesn't really fix your issue, the code will be better organized and you can trust 100% that the animation block is happening when the keyboard is showing (which you can't in `-textFieldShouldBeginEditing:`) and you can access the keyboard initial/final size and its animations properties. – Alberto S. Jun 28 '16 at 18:06
0

@lin fu

.m

    - (void)viewDidLoad {
    [super viewDidLoad];

    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = @"Click me!!";
    textField.delegate = self;
    textField.frame = CGRectMake(100, 350, 150, 40);
    [self.view addSubview:textField];


}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = textField.frame;
        frame.origin.y -= 100;
        textField.frame = frame;
    }];
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = textField.frame;
        frame.origin.y += 100;
        textField.frame = frame;
    }];
    return YES;
}

in .h

Try this out. It works for me.

jiten
  • 137
  • 8
0

you have another option you can use the third party library call TPAvoidingKeyBoard it will help you out in there

Rane
  • 1
  • 3