UIScrollView not scrolling in iOS7 with autolayout on(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 中不滚动自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 在 iOS7 中不滚动自动布局
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01