当键盘出现和消失时,如何在 iPhone 应用程序中UItextview
上下移动。UIScrollView
问问题
5149 次
4 回答
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];
}
更新:
斯威夫特 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
})
}
不要忘记删除观察者!!!
于 2013-07-12T09:40:50.633 回答
3
只需在-viewDidLoad
or中添加键盘操作的通知观察者-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:
然后在&-_keyboardWillHide:
方法中更新您的文本视图的框架。
于 2013-07-12T09:39:38.760 回答
2
有一个不错的库TPKeyboardAvoiding
,你可以使用(在这里找到)。
或者,您可以自己编程并使用 的contentOffset
属性UIScrollView
向下或向上移动内容。然后你应该听键盘的keyboardDidShow
和keyboardWillHide
方法。
希望能帮助到你!
于 2013-07-12T09:40:26.853 回答
-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;
}
于 2013-07-12T09:42:57.313 回答