Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • KeyboardWillShow and KeyboardWillHide

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 858
    Comment on it

    When we click on TextField  keyboard lifts up but if our text field is at bottom then we are not able to see it so with the help of NSNotificationCenter we can solve this issue.

    Add one TextField in ViewController, give bottom constraint to it and make the outlet of textfield and bottom constraint. In viewDidLoad write below line of code.

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:self.view.window]; //addObserver is used to receive the notifications.
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:[[self view] window]];
    
       
    }

     

    As in NSNotificationCenter we declare a method name keybaordWillShow so now we have to define it by using below code.

    - (void)keyboardWillShow:(NSNotification *)notification{
        
          NSDictionary* userInfo = [notification userInfo]; 
        NSLog(@"user Info is %@",userInfo);
        
        CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; //here we will got the keyboard size and with the help of this we can uplift the textfiled with the keyboard
        NSLog(@"keyboard size %f",keyboardSize.height);
        
        [UIView animateWithDuration:0.5 animations:^{
            _bottomConstraints.constant = keyboardSize.height;// _bottom constraint is the bottom constraint of the textfiled.
            [self.view layoutIfNeeded];
        }];
        
    }

     

    Now in keyboardWillHide method write below code

    - (void)keyboardWillHide:(NSNotification *)notification{
        
         [UIView animateWithDuration:0.5 animations:^{
            _bottomConstraints.constant = 97;
            [self.view layoutIfNeeded];
        }];
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: