UIScrollView 在 iOS7 中不滚动自动布局

UIScrollView not scrolling in iOS7 with autolayout on(UIScrollView 在 iOS7 中不滚动自动布局)
本文介绍了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 的frame,这样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)];

如果由于某些与自动布局相关的奇怪原因,这仍然无法使视图可滚动,请在 viewDidLayoutSubviews 中实现此 setContentSize 函数以覆盖自动布局:

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)];
}

要在关闭键盘后重置滚动视图,请将滚动视图内容大小重置为滚动视图的框架,并将偏移量重置为零:

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. To animate the content offset, use:

[scrollview setContentOffset:offsetSize animated:YES];

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)