I've looked at over a dozen of solutions on stackoverflow and some other sites, but I still can't solve this issue.

I'm currently learning how to use NSNotification and came across simple task - to move content when keyboard shows up. So I set up a simple single view app, added a TextField at the bottom and embedded it in a ScrollView that takes up the whole View. Here's the side question - is embedding using "Editor - Embed In" option the same as just putting a ScrollView first and then placing a TextField on top of it using IB? And would be the result the same if I added the textField to ScrollView programmatically using addSubview:? Is there a difference?

UI elements added as outlets

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

So, anyway, I'm trying to handle a keyboard notification like this

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{


        NSDictionary* info = [paramNotification userInfo];

        //float animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        self.scrollView.contentInset = contentInsets;
        self.scrollView.scrollIndicatorInsets = contentInsets;

        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;

        if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {

            [self.scrollView scrollRectToVisible:activeField.frame animated:YES];

        }

    NSLog(@"keyboardWillShow Notification Handled");
}

And literally nothing happens when keyboard appears, except log message, so notification is actually caught, but text field doesn't move.

What am I doing wrong? I feel like it's something obvious and I feel dumb already. I've actually tried some other solutions provided on this site, but most of them focuses on the Notification aspect and that part seems to work for me. This code snippet seemed as most simple to me.

Related posts

Recent Viewed