UItextview move up and down in iphone app( when keyboard appear and disappear)
本问题已经有最佳答案,请猛点这里访问。
当键盘出现或消失时,如何在iPhone应用程序中的
其中一种方法是——>在
1 2 3 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(keyboardDisappeared) name:UIKeyboardWillHideNotification object:nil]; [center addObserver:self selector:@selector(keyboardAppeared) name:UIKeyboardWillShowNotification object:nil]; |
将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | -(void) keyboardDisappeared { [UIView beginAnimations:@"animate" context:nil]; [UIView setAnimationDuration:0.2f]; [UIView setAnimationBeginsFromCurrentState: NO]; self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+100(same as you do in keyboardAppeared), self.view.frame.size.width, self.view.frame.size.height); [UIView commitAnimations]; } -(void) keyboardAppeared { [UIView beginAnimations:@"animate" context:nil]; [UIView setAnimationDuration:0.2f]; [UIView setAnimationBeginsFromCurrentState: NO]; self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-(as much you want), self.view.frame.size.width, self.view.frame.size.height); [UIView commitAnimations]; } |
更新:
斯威夫特3×4 / X
添加这些观察者-
1 2 | NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) |
以及选择器方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @objc func keyboardWillShow(notification:NSNotification) { adjustView(show: true, notification: notification) } @objc func keyboardWillHide(notification:NSNotification) { adjustView(show: false, notification: notification) } func adjustView(show:Bool, notification:NSNotification) { var userInfo = notification.userInfo! let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval let changeInHeight = (keyboardFrame.size.height + 40/*or any value as per need*/) * (show ? 1 : -1) UIView.animate(withDuration: animationDurarion, animations: { () -> Void in // change height Constraint or change height with changeInHeight here to your UItextView/other view }) } |
别忘了删除观察者!!!!
只需在
1 2 3 4 5 6 7 8 9 | NSNotificationCenter * notificationCetner = [NSNotificationCenter defaultCenter]; [notificationCetner addObserver:self selector:@selector(_keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [notificationCetner addObserver:self selector:@selector(_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; |
然后在
有一个叫
或者,您可以自己编程,并使用
希望它有帮助!
首先设置textview的委托,然后尝试实现这些方法….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | - (BOOL)textViewShouldBeginEditing:(UITextView *)textView { [self scrollViewToCenterOfScreen:textView]; return YES; } - (void) scrollViewToCenterOfScreen:(UIView *)theView { CGFloat viewCenterY = theView.center.y; CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; CGFloat availableHeight = applicationFrame.size.height - 300; // Remove area covered by keyboard CGFloat y = viewCenterY - availableHeight / 2.0; if (y < 0) { y = 0; } [scrollView setContentOffset:CGPointMake(0, y) animated:YES]; } // -------- Modified -------- - (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)tempText { if( [tempText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) { return YES; } [txtView resignFirstResponder]; [self.scrollView setContentOffset:CGPointMake(0.0f, 0.0f) animated:TRUE]; return NO; } |