ios UIScrollView 在 iOS7 中不滚动自动布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20317980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIScrollView not scrolling in iOS7 with autolayout on
提问by jdog
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.
我有一个 UIScrollView,里面有 6 个文本字段和一个按钮。scrollView 中没有足够的内容使其滚动。
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.
但是当键盘显示时,我希望滚动视图滚动,这样用户就不必关闭键盘来选择键盘隐藏的另一个文本字段。
I am using iOS7 and have autolayout enabled.
我正在使用 iOS7 并启用了自动布局。
Any suggestions?
有什么建议?
I am using storyboards and the only code I have is the following.
我正在使用故事板,我拥有的唯一代码如下。
reg.h file
.h 文件
interface registerViewController : UIViewController <UITextFieldDelegate, UIScrollViewDelegate>
回答by Lyndsey Scott
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:
为了使滚动视图可滚动,内容大小必须大于滚动视图的框架,以便滚动视图可以滚动到某些内容。使用 setContentSize 调整内容大小:
[scrollview setContentSize:CGSizeMake(width, 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:
在这种情况下,您应该将大小调整为view.frame.width,view.frame.height + keyboard_height,然后在键盘出现后调整内容偏移量:
[scrollview setContentOffset:CGPointMake(0, 0 - keyboard_height)];
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:
如果由于某些与自动布局相关的棘手原因,这仍然无法使视图可滚动,请在 viewDidLayoutSubviews 中实现此 setContentSize 函数以覆盖自动布局:
- (void)viewDidLayoutSubviews {
[scrollview setContentSize:CGSizeMake(width, height)];
}
EDIT: 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:
PS 要为内容偏移设置动画,请使用:
[scrollview setContentOffset:offsetSize animated:YES];
回答by mrt
There is a contentInset property of UIScrollViews, you can set the contentInset to make additional space at the bottom to allow for scrolling without changing contentSize.
UIScrollViews 有一个 contentInset 属性,你可以设置 contentInset 在底部腾出额外的空间,以便在不改变 contentSize 的情况下滚动。
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 100, 0.0);
scrollView.contentInset = contentInsets;
above code adds 100 points inset at the bottom.
上面的代码在底部添加了 100 个点。
By the way, there is an official document about this matter. It explains everything you should do. You can find it here. You can find what you are looking for under the section 'Moving Content That Is Located Under the Keyboard'
顺便说一下,官方有一份关于这件事的文件。它解释了你应该做的一切。你可以在这里找到它。您可以在“移动位于键盘下方的内容”部分下找到您要查找的内容
回答by Sreenath S
Try
尝试
- Create Scroll view
- Add View to the scroll view (In my case i added view as mainView).
- Set ScrollView autoresizing.
To set the Scroll content Size equal to the view created add below line
- 创建滚动视图
- 将视图添加到滚动视图(在我的情况下,我将视图添加为 mainView)。
- 设置 ScrollView 自动调整大小。
要将滚动内容大小设置为等于创建的视图,请添加以下行
Add the below line
添加以下行
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.scrollView.contentSize = self.mainView.frame.size;
}