我有一个带有textview和一个按钮的视图,就像在任何典型的消息传递应用程序中一样。出现键盘时,文本视图和按钮也被向上推,但内容被向上推离屏幕,在那里我看不到它的顶端,但是当我向下滚动时,我看到它向下蠕动并且向上弹起当我放手。我已经用快速草图说明了我遇到的问题,第三个屏幕是所需的行为。
我在.xib文件中有此视图。我有一个视图-> ScrollView-> contentView(它具有一个表以及textview和按钮)
我注册了键盘通知,这是show / hide方法的代码
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *) aNotification {
NSDictionary *info = [aNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// the hardcoded 49 is the height of the uitabbar
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view addGestureRecognizer:self.tapRecognizer];
}
- (void)keyboardWillBeHidden:(NSNotification *) aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[self.view removeGestureRecognizer:self.tapRecognizer];
}
最佳答案
我已经在keyboardWasShown方法中看到了这一行
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);
边缘插图的最高位置是-ve,因为49-keyboardSize(即appx 256)。替换为:
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0);
体验适合您的最佳计算套件。希望这项工作:)
关于ios - 向上推软键盘时如何更改UIScrollView内容的高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36805503/