我有一个 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 中不滚动自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Xcode 4 的 Interface Builder 中滚动 UIScrollViewScrolling through UIScrollView in Interface Builder for Xcode 4(在 Xcode 4 的 Interface Builder 中滚动 UIScrollView)
滑动删除嵌入在 UIScrollView 中的 UITableViewswipe to delete in a UITableView which is embeded in a UIScrollView(滑动删除嵌入在 UIScrollView 中的 UITableView)
检查 UIScrollView 中的滚动方向Check direction of scroll in UIScrollView(检查 UIScrollView 中的滚动方向)
UIScrollView 不滚动?UIScrollView Not Scrolling?(UIScrollView 不滚动?)
UIScrollView 如何从其子视图中窃取触摸?How does UIScrollView steal touches from its subviews?(UIScrollView 如何从其子视图中窃取触摸?)
使用自动布局将子视图的宽度与其父视图匹配Matching subview#39;s width to it#39;s superview using autolayout(使用自动布局将子视图的宽度与其父视图匹配)