设置了 contentsize 的 UIScrollView 无法滚动超出帧边界
Posted
技术标签:
【中文标题】设置了 contentsize 的 UIScrollView 无法滚动超出帧边界【英文标题】:UIScrollView with contentsize set can't scroll beyond frame boundary 【发布时间】:2013-09-24 21:00:02 【问题描述】:我觉得我在这里遗漏了一些非常明显的东西。
我在情节提要中有一个 UIScrollView,它有一个通向视图控制器的出口。
我以编程方式添加一系列视图,并设置滚动视图的内容大小。
即使我已将滚动视图的 contentsize 高度设置为非常高的值,我也无法滚动超出视图边界。
设置后我正在打印内容大小和帧大小。以及滚动视图滚动时的 contentoffset.y。
这是控制台输出的示例:
2013-09-24 21:54:34.531 [Redacted] scrollView frame size: 320.000000,460.000000
2013-09-24 21:54:34.531 [Redacted] scrollView content size: 0.000000,10000.000000
2013-09-24 21:54:36.259 [Redacted] scroll to: 6.000000
2013-09-24 21:54:36.275 [Redacted] scroll to: 17.000000
2013-09-24 21:54:36.291 [Redacted] scroll to: 29.000000
2013-09-24 21:54:36.308 [Redacted] scroll to: 41.000000
2013-09-24 21:54:36.325 [Redacted] scroll to: 52.000000
2013-09-24 21:54:36.341 [Redacted] scroll to: 65.000000
2013-09-24 21:54:36.358 [Redacted] scroll to: 74.000000
2013-09-24 21:54:36.375 [Redacted] scroll to: 84.000000
2013-09-24 21:54:36.392 [Redacted] scroll to: 90.000000
2013-09-24 21:54:36.408 [Redacted] scroll to: 95.000000
2013-09-24 21:54:36.425 [Redacted] scroll to: 100.000000
2013-09-24 21:54:36.441 [Redacted] scroll to: 103.000000
2013-09-24 21:54:36.459 [Redacted] scroll to: 107.000000
2013-09-24 21:54:36.475 [Redacted] scroll to: 110.000000
2013-09-24 21:54:36.492 [Redacted] scroll to: 114.000000
2013-09-24 21:54:36.509 [Redacted] scroll to: 118.000000
2013-09-24 21:54:36.525 [Redacted] scroll to: 122.000000
2013-09-24 21:54:36.542 [Redacted] scroll to: 126.000000
2013-09-24 21:54:36.558 [Redacted] scroll to: 130.000000
2013-09-24 21:54:36.575 [Redacted] scroll to: 134.000000
2013-09-24 21:54:36.592 [Redacted] scroll to: 141.000000
2013-09-24 21:54:36.609 [Redacted] scroll to: 147.000000
2013-09-24 21:54:36.625 [Redacted] scroll to: 152.000000
2013-09-24 21:54:36.642 [Redacted] scroll to: 156.000000
2013-09-24 21:54:36.659 [Redacted] scroll to: 159.000000
2013-09-24 21:54:36.676 [Redacted] scroll to: 160.000000
2013-09-24 21:54:36.776 [Redacted] scroll to: 161.000000
2013-09-24 21:54:37.010 [Redacted] scroll to: 162.000000
2013-09-24 21:54:37.026 [Redacted] scroll to: 168.000000
2013-09-24 21:54:37.043 [Redacted] scroll to: 179.000000
2013-09-24 21:54:37.060 [Redacted] scroll to: 195.000000
2013-09-24 21:54:37.077 [Redacted] scroll to: 207.000000
2013-09-24 21:54:37.097 [Redacted] scroll to: 209.000000
2013-09-24 21:54:37.112 [Redacted] scroll to: 219.000000
2013-09-24 21:54:37.129 [Redacted] scroll to: 223.000000
2013-09-24 21:54:37.145 [Redacted] scroll to: 222.000000
2013-09-24 21:54:37.162 [Redacted] scroll to: 218.000000
2013-09-24 21:54:37.179 [Redacted] scroll to: 212.000000
2013-09-24 21:54:37.195 [Redacted] scroll to: 204.000000
2013-09-24 21:54:37.212 [Redacted] scroll to: 195.000000
2013-09-24 21:54:37.229 [Redacted] scroll to: 186.000000
2013-09-24 21:54:37.245 [Redacted] scroll to: 177.000000
2013-09-24 21:54:37.262 [Redacted] scroll to: 168.000000
2013-09-24 21:54:37.279 [Redacted] scroll to: 160.000000
2013-09-24 21:54:37.296 [Redacted] scroll to: 152.000000
2013-09-24 21:54:37.312 [Redacted] scroll to: 145.000000
2013-09-24 21:54:37.329 [Redacted] scroll to: 138.000000
2013-09-24 21:54:37.346 [Redacted] scroll to: 133.000000
2013-09-24 21:54:37.362 [Redacted] scroll to: 127.000000
2013-09-24 21:54:37.379 [Redacted] scroll to: 122.000000
2013-09-24 21:54:37.396 [Redacted] scroll to: 118.000000
2013-09-24 21:54:37.412 [Redacted] scroll to: 114.000000
2013-09-24 21:54:37.429 [Redacted] scroll to: 111.000000
2013-09-24 21:54:37.445 [Redacted] scroll to: 108.000000
2013-09-24 21:54:37.462 [Redacted] scroll to: 105.000000
2013-09-24 21:54:37.479 [Redacted] scroll to: 103.000000
2013-09-24 21:54:37.496 [Redacted] scroll to: 101.000000
2013-09-24 21:54:37.512 [Redacted] scroll to: 99.000000
2013-09-24 21:54:37.529 [Redacted] scroll to: 98.000000
2013-09-24 21:54:37.545 [Redacted] scroll to: 96.000000
2013-09-24 21:54:37.562 [Redacted] scroll to: 95.000000
2013-09-24 21:54:37.579 [Redacted] scroll to: 94.000000
2013-09-24 21:54:37.595 [Redacted] scroll to: 93.000000
2013-09-24 21:54:37.629 [Redacted] scroll to: 92.000000
2013-09-24 21:54:37.645 [Redacted] scroll to: 91.000000
2013-09-24 21:54:37.695 [Redacted] scroll to: 90.000000
2013-09-24 21:54:37.745 [Redacted] scroll to: 89.000000
2013-09-24 21:54:37.862 [Redacted] scroll to: 88.000000
2013-09-24 21:54:38.381 [Redacted] scroll to: 89.000000
2013-09-24 21:54:38.397 [Redacted] scroll to: 108.000000
2013-09-24 21:54:38.414 [Redacted] scroll to: 127.000000
2013-09-24 21:54:38.431 [Redacted] scroll to: 150.000000
2013-09-24 21:54:38.448 [Redacted] scroll to: 164.000000
2013-09-24 21:54:38.464 [Redacted] scroll to: 182.000000
2013-09-24 21:54:38.481 [Redacted] scroll to: 195.000000
2013-09-24 21:54:38.499 [Redacted] scroll to: 213.000000
2013-09-24 21:54:38.516 [Redacted] scroll to: 222.000000
2013-09-24 21:54:38.533 [Redacted] scroll to: 224.000000
2013-09-24 21:54:38.550 [Redacted] scroll to: 222.000000
2013-09-24 21:54:38.566 [Redacted] scroll to: 217.000000
2013-09-24 21:54:38.583 [Redacted] scroll to: 209.000000
2013-09-24 21:54:38.599 [Redacted] scroll to: 201.000000
2013-09-24 21:54:38.616 [Redacted] scroll to: 192.000000
2013-09-24 21:54:38.633 [Redacted] scroll to: 183.000000
2013-09-24 21:54:38.650 [Redacted] scroll to: 174.000000
2013-09-24 21:54:38.666 [Redacted] scroll to: 166.000000
2013-09-24 21:54:38.683 [Redacted] scroll to: 157.000000
2013-09-24 21:54:38.699 [Redacted] scroll to: 150.000000
2013-09-24 21:54:38.716 [Redacted] scroll to: 143.000000
2013-09-24 21:54:38.733 [Redacted] scroll to: 137.000000
2013-09-24 21:54:38.749 [Redacted] scroll to: 131.000000
2013-09-24 21:54:38.766 [Redacted] scroll to: 126.000000
2013-09-24 21:54:38.783 [Redacted] scroll to: 121.000000
2013-09-24 21:54:38.799 [Redacted] scroll to: 117.000000
2013-09-24 21:54:38.816 [Redacted] scroll to: 113.000000
2013-09-24 21:54:38.833 [Redacted] scroll to: 110.000000
2013-09-24 21:54:38.849 [Redacted] scroll to: 107.000000
2013-09-24 21:54:38.866 [Redacted] scroll to: 105.000000
2013-09-24 21:54:38.883 [Redacted] scroll to: 102.000000
2013-09-24 21:54:38.900 [Redacted] scroll to: 100.000000
2013-09-24 21:54:38.916 [Redacted] scroll to: 99.000000
2013-09-24 21:54:38.933 [Redacted] scroll to: 97.000000
2013-09-24 21:54:38.950 [Redacted] scroll to: 96.000000
2013-09-24 21:54:38.966 [Redacted] scroll to: 95.000000
2013-09-24 21:54:38.983 [Redacted] scroll to: 94.000000
2013-09-24 21:54:39.000 [Redacted] scroll to: 93.000000
2013-09-24 21:54:39.016 [Redacted] scroll to: 92.000000
2013-09-24 21:54:39.049 [Redacted] scroll to: 91.000000
2013-09-24 21:54:39.083 [Redacted] scroll to: 90.000000
2013-09-24 21:54:39.150 [Redacted] scroll to: 89.000000
2013-09-24 21:54:39.267 [Redacted] scroll to: 88.000000
2013-09-24 21:54:43.145 [Redacted] scroll to: 91.000000
2013-09-24 21:54:43.162 [Redacted] scroll to: 97.000000
2013-09-24 21:54:43.178 [Redacted] scroll to: 103.000000
2013-09-24 21:54:43.195 [Redacted] scroll to: 109.000000
2013-09-24 21:54:43.212 [Redacted] scroll to: 115.000000
2013-09-24 21:54:43.229 [Redacted] scroll to: 120.000000
2013-09-24 21:54:43.245 [Redacted] scroll to: 124.000000
2013-09-24 21:54:43.262 [Redacted] scroll to: 127.000000
2013-09-24 21:54:43.279 [Redacted] scroll to: 130.000000
2013-09-24 21:54:43.296 [Redacted] scroll to: 131.000000
2013-09-24 21:54:43.312 [Redacted] scroll to: 133.000000
2013-09-24 21:54:43.329 [Redacted] scroll to: 135.000000
2013-09-24 21:54:43.345 [Redacted] scroll to: 136.000000
2013-09-24 21:54:43.374 [Redacted] scroll to: 137.000000
2013-09-24 21:54:43.664 [Redacted] scroll to: 138.000000
2013-09-24 21:54:43.681 [Redacted] scroll to: 139.000000
2013-09-24 21:54:43.697 [Redacted] scroll to: 143.000000
2013-09-24 21:54:43.714 [Redacted] scroll to: 149.000000
2013-09-24 21:54:43.731 [Redacted] scroll to: 155.000000
2013-09-24 21:54:43.748 [Redacted] scroll to: 161.000000
2013-09-24 21:54:43.764 [Redacted] scroll to: 166.000000
2013-09-24 21:54:43.781 [Redacted] scroll to: 169.000000
2013-09-24 21:54:43.798 [Redacted] scroll to: 172.000000
2013-09-24 21:54:43.814 [Redacted] scroll to: 174.000000
2013-09-24 21:54:43.831 [Redacted] scroll to: 177.000000
2013-09-24 21:54:43.848 [Redacted] scroll to: 178.000000
2013-09-24 21:54:43.865 [Redacted] scroll to: 180.000000
2013-09-24 21:54:43.887 [Redacted] scroll to: 181.000000
2013-09-24 21:54:43.914 [Redacted] scroll to: 183.000000
2013-09-24 21:54:43.948 [Redacted] scroll to: 184.000000
2013-09-24 21:54:43.981 [Redacted] scroll to: 186.000000
2013-09-24 21:54:43.998 [Redacted] scroll to: 187.000000
2013-09-24 21:54:44.015 [Redacted] scroll to: 188.000000
2013-09-24 21:54:44.031 [Redacted] scroll to: 189.000000
2013-09-24 21:54:44.048 [Redacted] scroll to: 190.000000
2013-09-24 21:54:44.065 [Redacted] scroll to: 191.000000
2013-09-24 21:54:44.082 [Redacted] scroll to: 192.000000
2013-09-24 21:54:44.116 [Redacted] scroll to: 194.000000
2013-09-24 21:54:44.149 [Redacted] scroll to: 195.000000
2013-09-24 21:54:44.182 [Redacted] scroll to: 196.000000
2013-09-24 21:54:44.216 [Redacted] scroll to: 197.000000
2013-09-24 21:54:44.266 [Redacted] scroll to: 198.000000
2013-09-24 21:54:44.283 [Redacted] scroll to: 199.000000
2013-09-24 21:54:44.299 [Redacted] scroll to: 200.000000
2013-09-24 21:54:44.316 [Redacted] scroll to: 202.000000
2013-09-24 21:54:44.349 [Redacted] scroll to: 203.000000
2013-09-24 21:54:44.366 [Redacted] scroll to: 204.000000
2013-09-24 21:54:44.449 [Redacted] scroll to: 205.000000
2013-09-24 21:54:44.500 [Redacted] scroll to: 206.000000
2013-09-24 21:54:44.601 [Redacted] scroll to: 207.000000
2013-09-24 21:54:44.932 [Redacted] scroll to: 189.000000
2013-09-24 21:54:44.949 [Redacted] scroll to: 174.000000
2013-09-24 21:54:44.966 [Redacted] scroll to: 161.000000
2013-09-24 21:54:44.982 [Redacted] scroll to: 151.000000
2013-09-24 21:54:44.999 [Redacted] scroll to: 141.000000
2013-09-24 21:54:45.016 [Redacted] scroll to: 133.000000
2013-09-24 21:54:45.032 [Redacted] scroll to: 126.000000
2013-09-24 21:54:45.049 [Redacted] scroll to: 121.000000
2013-09-24 21:54:45.066 [Redacted] scroll to: 116.000000
2013-09-24 21:54:45.082 [Redacted] scroll to: 112.000000
2013-09-24 21:54:45.099 [Redacted] scroll to: 108.000000
2013-09-24 21:54:45.116 [Redacted] scroll to: 105.000000
2013-09-24 21:54:45.133 [Redacted] scroll to: 102.000000
2013-09-24 21:54:45.149 [Redacted] scroll to: 100.000000
2013-09-24 21:54:45.166 [Redacted] scroll to: 98.000000
2013-09-24 21:54:45.183 [Redacted] scroll to: 97.000000
2013-09-24 21:54:45.200 [Redacted] scroll to: 96.000000
2013-09-24 21:54:45.216 [Redacted] scroll to: 94.000000
2013-09-24 21:54:45.233 [Redacted] scroll to: 93.000000
2013-09-24 21:54:45.266 [Redacted] scroll to: 92.000000
2013-09-24 21:54:45.283 [Redacted] scroll to: 91.000000
2013-09-24 21:54:45.316 [Redacted] scroll to: 90.000000
2013-09-24 21:54:45.366 [Redacted] scroll to: 89.000000
2013-09-24 21:54:45.483 [Redacted] scroll to: 88.000000
如你所见,我无法滚动超过 200 个。
我对 ios 并不陌生,之前使用过滚动视图,所以我觉得我一定错过了一些非常明显的东西。
非常感谢任何帮助,截止日期很紧!
编辑:添加代码:
checkboxes = [[NSMutableArray alloc] init];
// Just indented scope for tidyness, no functional meaning
[checkboxes addObject:[self createCheckboxWithLabel:@"Oil Level Checked"
atY:190]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Coolant Level Checked"
atY:240]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Tyre pressures & tread dept checked"
atY:290]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Warning beacon & horn operating"
atY:340]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Lights & indicators working & clean"
atY:390]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Screen was & wipers operational"
atY:440]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Mirrors undamaged & clean"
atY:490]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Road Tax & MOD discs present & valid"
atY:540]];
[checkboxes addObject:[self createCheckboxWithLabel:@"PPE in vechile"
atY:590]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Fire Extinguisher & First Aid kit present"
atY:640]];
[checkboxes addObject:[self createCheckboxWithLabel:@"Wheel nuts checked with torque wrench"
atY:690]];
for(WpCheckboxWithLabelView *checkbox in checkboxes)
[scrollView addSubview: checkbox];
// Set content size of scrollview
scrollView.contentSize = CGSizeMake(0, 10000);
scrollView.delegate = self;
NSLog(@"scrollView frame size: %f,%f", scrollView.frame.size.width, scrollView.frame.size.height);
NSLog(@"scrollView content size: %f,%f", scrollView.contentSize.width, scrollView.contentSize.height);
【问题讨论】:
【参考方案1】:我的观点和我的观点相同,它对我来说很好用。我附上了代码:
CGSize s = CGSizeMake(mainDetailView.frame.size.width,
view1.frame.size.height + MARGIN +
view2.frame.size.height + MARGIN +
view3.frame.size.height + MARGIN +
view4.frame.size.height + MARGIN + 10000);
[mainDetailView setContentSize:s];
mainDetailView.clipsToBounds = NO;
mainDetailView.pagingEnabled = NO;
Margin 是一个常数,我定义为 20。
我可以滚动超出视图的限制。
也许您可以发布您的代码,这样会更容易为您提供帮助。
【讨论】:
我已将代码添加到 OP。我只是创建了一系列自定义视图。它们超出了滚动视图框架,所以我将内容大小设置得很大,但我仍然无法滚动到框架之外。 (如 OP 中的控制台输出所示。)【参考方案2】:自动布局是这里问题的原因。我不太清楚细节,但是当与自动布局配对时,Apple's documentation describes the behavioural differences 的 UIScrollView
s。
我相信我的问题是由于在故事板中定义的滚动视图和以编程方式添加的子视图。为了解决这个问题,我还以编程方式创建了UIScrollView
。
【讨论】:
以上是关于设置了 contentsize 的 UIScrollView 无法滚动超出帧边界的主要内容,如果未能解决你的问题,请参考以下文章
将 XIB 的 UIScrollView 添加到视图时设置 contentSize
在 iOS6、XCode 4.5 中,UIScrollView 不滚动,尽管设置了 contentSize
将 textView 高度设置为等于 contentSize swift
即使 contentSize 大于 frame 并且在设置内容大小之前添加了子视图,滚动视图也不起作用