Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

ios - UITableView and UIView with keyboardWillShow

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...