[英]UIScrollView setContentOffset will add a subview to the scrollview?
我碰巧发现setContentOffset到UIScrollView将导致scrollView将新视图附加到其子视图。 这是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((320 - 261) / 2, 50, 261, 67)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.backgroundColor = [UIColor darkGrayColor];
for (int i = 0; i < 5; i ++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 87, 1, 87, 65)];
view.tag = i;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 30)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"%d", i];
[view addSubview:label];
view.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view];
}
scrollView.contentSize = CGSizeMake(87 * 5, 67);
NSLog(@"before scroll: count of subview is %d", scrollView.subviews.count);
CGPoint offset = CGPointMake(87, 0);
[scrollView setContentOffset:offset animated:YES];
NSLog(@"after scroll: count of subview is %d", scrollView.subviews.count);
[self.view addSubview:scrollView];
}
在调用setContentOffset:offset
之前,scrollView的子视图数是5.这就是我所期望的。 之后,数字变为6.它是否按设计工作? 如何避免附加新的子视图?
额外的子视图是滚动指示器。 如果你在以后的方法中再次检查,它将会消失。 别担心。
您可以通过隐藏滚动指示符( showsVerticalScrollIndicator
和showsHorizontalScrollIndicator
属性)来确认这一点。
不要试图假设有关UIKit类的视图层次结构的事情。 UIKit可以并确实将自己的视图添加到几个方面 - 请参阅tableviews及其单元格,导航控制器的视图等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.