Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to move UITextField when keyboard shows ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 517
    Comment on it

    When we are entering text in a textField and that textField is at bottom of the screen then we need that the textField should appear above the keyboard and the keyboard should not hide the textField.. For that we need to set the height of keyboard according to the selected textField..

    For implementing this first we have to fetch value from the NSNotificationCenter. Its will tell how the keyboard will appear.. Implement the below code in the viewDidLoad method :-

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    

     Define the methods :-

    - (void)keyboardWillShow: (NSNotification *) notif{
       
        NSLog(@"%@",notif);
        NSDictionary *dictionary = [notif userInfo];
        NSValue* keyboardFrameBegin = [dictionary valueForKey:UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
       
       _bottomConstraint.constant = keyboardFrameBeginRect.size.height;      
         
     UIViewAnimationCurve curve = [[dictionary objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; // keyboard will appear animated
        double duration = [[dictionary objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        [UIView animateWithDuration:duration delay:0.5 options:UIViewAnimationOptionBeginFromCurrentState animations:^{  // completion block used
                             [UIView setAnimationCurve:curve];
            
                         }
        completion:nil];
    
    }
    

    _bottomConstraint is the constraint from the bottom of textField to the superview.

    In the below method we are setting constant value from the bottom when the keyboard will drop down or hide .

     (void)keyboardWillHide: (NSNotification *) notif{
        
        _bottomConstraint.constant = 20;   // set the constant value from bottom
          
    }

    Its done !!!

 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: