UIScrollView在自动布局打开时不在iOS7中滚动 [英] UIScrollView not scrolling in iOS7 with autolayout on

查看:95
本文介绍了UIScrollView在自动布局打开时不在iOS7中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView,里面有6个文本字段,里面有一个按钮。 scrollView中没有足够的内容使其滚动。

I have a UIScrollView with a 6 textfields in it and a button inside of it. There is not enough content in the scrollView to make it scroll.

但是当键盘显示时,我希望滚动视图滚动,这样用户就不必关闭键盘,以便选择键盘隐藏的另一个文本字段。

But when the keyboard shows, I would like the scrollview to scroll so the user doesn't have to dismiss the keyboard in order to select another textfield that is hidden by the keyboard.

我正在使用iOS7并启用了自动布局。

I am using iOS7 and have autolayout enabled.

有任何建议吗?

我使用的是故事板,我所拥有的唯一代码如下。

I am using storyboards and the only code I have is the following.

reg.h文件

interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>


推荐答案

为了使滚动视图可滚动,内容大小必须大于scrollview的框架,因此scrollview有滚动到的东西。使用setContentSize调整内容大小:

In order to make a scrollview scrollable, the content size must be larger than the scrollview's frame so the scrollview has something to scroll to. Use setContentSize to adjust the content size:

[scrollview setContentSize:CGSizeMake(width, height)];

在这种情况下,您应该将大小调整为view.frame.width,view.frame.height + keyboard_height,然后在键盘出现后调整内容偏移量:

In this case, you should adjust the size to view.frame.width, view.frame.height + keyboard_height, then adjust the content offset once the keyboard appears:

[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];

如果对于一些棘手的,与autolayout相关的原因,这仍然不能使视图可滚动,请实现此viewDidLayoutSubviews中的setContentSize函数,以覆盖autolayout:

If for some screwy, autolayout-related reason this still doesn't make the view scrollable, implement this setContentSize function in viewDidLayoutSubviews in order to override the autolayout:

- (void)viewDidLayoutSubviews {
     [scrollview setContentSize:CGSizeMake(width, height)];
}

编辑:要在解除键盘后重置滚动视图,请重置scrollview内容大小到scrollview的框架,偏移量为零:

To reset the scrollview after dismissing the keyboard, reset the scrollview content size to the scrollview's frame and the offset to zero:

[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, scrollview.frame.size.height)];
[scrollview setContentOffset:CGPointZero];

P.S。要设置内容偏移的动画,请使用:

P.S. To animate the content offset, use:

[scrollview setContentOffset:offsetSize animated:YES];

这篇关于UIScrollView在自动布局打开时不在iOS7中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆