当键盘出现和消失时,如何在iphone应用中UItextview
上下移动UIScrollView
。
答案 0 :(得分:5)
其中一个方法是---&{; viewDidLoad
或ViewWillAppear
添加这些观察者
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardDisappeared) name:UIKeyboardWillHideNotification object:nil];
[center addObserver:self selector:@selector(keyboardAppeared) name:UIKeyboardWillShowNotification object:nil];
而不是写下keyboardDisappeared
和keyboardAppeared
-(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];
}
<强>更新强>
SWIFT 3.x / 4.x
添加这些观察者 -
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)
选择方法:
@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 :(得分:3)
只需在-viewDidLoad
或-viewWillAppear:
中添加键盘操作的通知观察者:
NSNotificationCenter * notificationCetner = [NSNotificationCenter defaultCenter];
[notificationCetner addObserver:self
selector:@selector(_keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[notificationCetner addObserver:self
selector:@selector(_keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
然后在-_keyboardWasShown:
&amp;更新文本视图的框架-_keyboardWillHide:
方法。
答案 2 :(得分:2)
您可以使用一个名为TPKeyboardAvoiding
的好库(找到它here)。
或者,您可以自行编程并使用contentOffset
的{{1}}属性向下或向上移动内容。然后,您应该正在聆听键盘的UIScrollView
和keyboardDidShow
方法。
希望它有所帮助!
答案 3 :(得分:-1)
首先设置textview的委托,然后尝试实现这些方法....
- (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;
}