【发布时间】:2014-11-10 03:56:39
【问题描述】:
我已将我所有的视图嵌入到 xib 的 UIScrollView 中。滚动视图内容覆盖状态栏下方的所有屏幕。现在,当点击文本字段时,我可以将滚动视图向上移动一点。但我希望它完全可滚动,直到键盘上方也可以看到最底部的视图。此外,当滚动视图滚动到 top 时,它应该会回到正常的原始位置。因此,总的来说,我想要一个完全可滚动的功能,就像上面提到的那样用于我的滚动视图。
我完成了以下技巧,但没有运气:
技巧 1:更改滚动视图的高度,使内容大于滚动视图高度,从而使视图可滚动:
-(void)keyboardWillAppear:(NSNotification *)sender
{
CGFloat y_offset=0;
if([UIScreen mainScreen].bounds.size.height == 480){
y_offset = 80;
} else {
y_offset = 70;
}
NSDictionary* userInfo = [sender userInfo];
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
keyboardHeight = keyboardEndFrame.size.height;
[UIView animateWithDuration:0.5f animations:^{
[self.view setFrame:CGRectMake(0, - y_offset, self.view.frame.size.width, self.view.frame.size.height)];
}];
[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height - keyboardHeight)];
}
-(void)keyboardWillDisappear:(NSNotification *)sender
{
[UIView animateWithDuration:0.5f animations:^{
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
}
技巧 2:根据其他建议,我更改了 UIScrollView 的 contentInset。
在keyboardWillAppear 方法中我添加了以下代码:
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0);
self.loginScrollView.contentInset = contentInsets;
self.loginScrollView.scrollIndicatorInsets = contentInsets;
在 keyboardWillDisappear 方法中,我将 contentInset 设置回零值。
因此,让我知道是否需要任何其他方式来解决这个问题或我需要在滚动视图框架中进行任何其他可能的更改。此外,如果我打开bouncesVertically 功能,即使我不想要的完整子视图在屏幕上可见,它也能够反弹。所以基本上我希望它在键盘不存在时冻结,并且在它启动时可以滚动到可见区域。因此,给我任何其他建议吗?提前致谢。
【问题讨论】:
-
在您的第一种方法中,将滚动视图 contentSize 设置为等于您的全屏尺寸(不存在键盘)以及框架调整以查看您已经执行的操作。跨度>
-
是的,这就像魅力!谢谢@甘道夫。请将其发布为答案,以便每个人都知道解决方案,我将接受相同的解决方案。 :)
-
很高兴知道它有帮助。我已经发布了答案。你可以接受。 :)
标签: ios objective-c uiscrollview keyboard frame