如何在软件键盘向上推动时更改UIScrollView内容的高度
问题描述:
我有一个带有textview和按钮的视图,就像在任何典型的消息应用程序中一样。当键盘出现时,文本视图和按钮也被向上推,但是内容被向上推离屏幕,在那里我没有看到它的顶端,但当我强制向下滚动时,我看到它爬下来,它向上弹当我放手的时候。我已经用快速草图说明了我遇到的问题,第三个屏幕是所需的行为。如何在软件键盘向上推动时更改UIScrollView内容的高度
我在.xib文件中有这个视图。我有一个观点 - >滚动型 - >内容查看(其中有一个表,并TextView的和按钮)
我注册键盘的通知,这里是为显示/隐藏方法的代码
- (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];
}
答
试试这个。
// Called when the UIKeyboardWillShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGPoint scrollPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom);
[scrollView setContentOffset:scrollPoint animated:true];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
答
我已经看到了keyboardWasShown方法这一行
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);
你在哪里顶边插图是-ve,becouse 49 - keyboardSize(这是APPX 256)。用此替代它:
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0);
玩计算最佳套件给你。希望这项工作:)
不幸的是没有为我工作。文本视图和按钮随滚动视图一起移动。 – ioskaveen
请尝试编辑答案 –
这也行不通。我想要做的是适合软件键盘之间的导航栏下的视图顶部的滚动视图 – ioskaveen