jdo*_*dog 19 objective-c uiscrollview ios
我有一个UIScrollView,里面有6个文本字段,里面有一个按钮.scrollView中没有足够的内容使其滚动.
但是当键盘显示时,我希望滚动视图滚动,这样用户就不必关闭键盘以选择键盘隐藏的另一个文本字段.
我正在使用iOS7并启用了autolayout.
有什么建议?
我正在使用故事板,我所拥有的唯一代码如下.
reg.h文件
interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>
Run Code Online (Sandbox Code Playgroud)
Lyn*_*ott 75
为了使滚动视图可滚动,内容大小必须大于scrollview的框架,因此scrollview可以滚动到某些内容.使用setContentSize调整内容大小:
[scrollview setContentSize:CGSizeMake(width, height)];
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您应该调整view.frame.width,view.frame.height + keyboard_height的大小,然后在键盘出现后调整内容偏移量:
[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];
Run Code Online (Sandbox Code Playgroud)
如果对于一些棘手的,与autolayout相关的原因,这仍然不能使视图可滚动,请在viewDidLayoutSubviews中实现此setContentSize函数以覆盖autolayout:
- (void)viewDidLayoutSubviews {
[scrollview setContentSize:CGSizeMake(width, height)];
}
Run Code Online (Sandbox Code Playgroud)
编辑:要在解除键盘后重置滚动视图,请将scrollview内容大小重置为scrollview的帧并将偏移量重置为零:
[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, scrollview.frame.size.height)];
[scrollview setContentOffset:CGPointZero];
Run Code Online (Sandbox Code Playgroud)
PS要为内容偏移设置动画,请使用:
[scrollview setContentOffset:offsetSize animated:YES];
Run Code Online (Sandbox Code Playgroud)
mrt*_*mrt 16
UIScrollViews有一个contentInset属性,您可以设置contentInset以在底部创建额外的空间以允许滚动而不更改contentSize.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 100, 0.0);
scrollView.contentInset = contentInsets;
Run Code Online (Sandbox Code Playgroud)
上面的代码在底部添加了100点插入.
顺便说一下,有关于此事的官方文件.它解释了你应该做的一切.你可以在这里找到它.您可以在"移动位于键盘下方的内容"部分找到您要查找的内容