2

I have this UITableView that almost fills my entire UIViewController, and I have a UIView at the bottom that contains a button and a textfield.

When I click the textfield, I want the UIView and tableview to push up, so that the UIView is just on top of the keyboard.

- UIView:  
  - UITextField
  - UIButton

I've tried multiple suggestions on on here, but none seem to work in my situation.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • 1
    did you try to add an outlet to the bottom constraint of your UIView and update it when the keyboard is displayed – Alaeddine Jul 11 '15 at 10:51
  • even better is to adjust the contentInsets of the scrollView in the keyboard notifications as suggested by apple. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html. check the section **Handling the keyboard notifications**. – GoGreen Apr 07 '16 at 06:41

3 Answers3

17

Step 1:
Make an outlet of bottom constraint of UIView

enter image description here

Step 2:
Add observer for keyboard show and hide and then change constraint constant according to keyboard height..

//**In viewDidLoad method** 

    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification 
                                               object:nil];  

Step 2 in Swift 5:

//**In viewDidLoad method** 

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

Step 3:
Manage constraints as keyboard show and hide notification like below

- (void)keyboardWillShow:(NSNotification *)notification
{

    NSDictionary* userInfo = [notification userInfo];

   // get the size of the keyboard
   CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

   CGSize keyboardSizeNew = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

   _bottomConstraintofView.constant = keyboardSizeNew.height;

  [UIView animateWithDuration:0.2
    animations:^{
        [self.view layoutIfNeeded]; // Called on parent view
    }];
 }

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

Solution in Swift

func keyboardWillShow(notification: NSNotification){
    let userInfo:NSDictionary = notification.userInfo!
    let keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
    
    let keyboardSizeNow:CGSize = userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue().size

    self.bottomConstraintofView.constant = keyboardSizeNow.height
    UIView.animateWithDuration(0.2) {  
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification){
    bottomConstraintofView.constant = 0
    UIView.animateWithDuration(0.2) {
        self.view.layoutIfNeeded()
    }
}
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
  • i have a custom NavigationBar.Is custom navigationBar will also move up as per u solution @EI Captain v2.0 – Dilip Tiwari Apr 05 '18 at 13:04
  • @DilipTiwari No, it will not move if u provide height constraint to that navbar and just left, right, top and bottom constraint to table view and at last constraints to view as shown above in answer. So it just moved bottom view and adjust height to table view but navbar remains same – Bhavin Bhadani Apr 05 '18 at 13:07
  • could u help me with my question https://stackoverflow.com/questions/49670145/autoayout-issue-swift-3-0 – Dilip Tiwari Apr 05 '18 at 13:08
  • @DilipTiwari I already gives u explanation in above comment .. just disable iqmanager as do as above comment – Bhavin Bhadani Apr 05 '18 at 13:09
  • i need solution for that i want navigationBar to stick at top and when i enter text tableview als move up but not navigationbar – Dilip Tiwari Apr 05 '18 at 13:09
  • @EICaptainv2.0 could u provide the answer of above link – Dilip Tiwari Apr 05 '18 at 13:11
  • i m having tabbar also – Dilip Tiwari Apr 05 '18 at 13:13
  • Still, it's work. Just follow the instruction and if u don't want to follow then I don't have another answer to help u. I already tried to help you but you don't want to implement. – Bhavin Bhadani Apr 05 '18 at 13:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168336/discussion-between-dilip-tiwari-and-ei-captain-v2-0). – Dilip Tiwari Apr 05 '18 at 18:14
  • @EICaptainv2.0 hii bro need help in this question nobody answer this https://stackoverflow.com/questions/51281675/how-to-create-top-navigation-like-reddit-app-in-swift-3 – Dilip Tiwari Jul 11 '18 at 11:51
0

As mentioned in the comment, connect your bottom constraint (the one from your view containing the textfield and the button) per @IBOutlet to your view controller. Listen to UIKeyboardWillHideNotification and UIKeyboardWillShowNotification and implement their selectors. When the keyboard appears adjust the bottom constraint to the keyboard height and when it hides set it back to 0 (or whatever value you have there). I would wrap the adjustment in an animation.

Like (in Swift):

func keyboardWillShow(notification: NSNotification) {
    var info = notification.userInfo!
    var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    self.view.layoutIfNeeded()
    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardFrame.size.height
        self.view.layoutIfNeeded()
    })
}

func keyboardWillHide(notification: NSNotification) {
    self.view.layoutIfNeeded()
    UIView.animateWithDuration(0.25, animations: { () -> Void in
        self.bottomConstraint.constant = 0
        self.view.layoutIfNeeded()
    })
}
matthias
  • 947
  • 1
  • 9
  • 27
0

One word: constraints.

Have a read of my article here: Height of iOS onscreen keyboard

It basically has a constraint at the bottom of the screen, and whenever the user opens the onscreen keyboard, it changes the height of this constaint.

enter image description here

Hope this helps.

Community
  • 1
  • 1
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159