为什么在应用程序设置时 contentview 的来源与 scrollview 的来源不一致?
Why the origin of contentview doesn't coincide the origin of scrollview when application setup?
The problem shows in the gif picture.(黄色的view是contentview,黑色的view是scrollview。)
The whole project code on Github.
这是代码:
CGRect frame = CGRectMake( 50, 100, 200, 200);
UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:scrollView];
frame= CGRectMake( 0, 0, 500, 500);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500,500);
scrollView.backgroundColor = [UIColor blackColor];
myImageView.backgroundColor = [UIColor yellowColor];
scrollView.contentOffset = CGPointMake(0, 0)
图中还有一个问题:
为什么水平滚动条不在滚动视图的底部?
当我在滚动视图和 self.view 之间添加一个视图时,问题就消失了。
我添加的代码:
UIView *view = [UIView new];
[self.view addSubview:view];
我发现这个问题是在我使用UITabBarController的时候出现的
使 UIScrollView 成为全局变量。像这样加载视图时,使 UIScrollView 的 contentInset 和 UIScrollView 的指示符 UIEdgeInsetsZero:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
The problem shows in the gif picture.(黄色的view是contentview,黑色的view是scrollview。)
The whole project code on Github.
这是代码:
CGRect frame = CGRectMake( 50, 100, 200, 200);
UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:scrollView];
frame= CGRectMake( 0, 0, 500, 500);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500,500);
scrollView.backgroundColor = [UIColor blackColor];
myImageView.backgroundColor = [UIColor yellowColor];
scrollView.contentOffset = CGPointMake(0, 0)
图中还有一个问题: 为什么水平滚动条不在滚动视图的底部?
当我在滚动视图和 self.view 之间添加一个视图时,问题就消失了。
我添加的代码:
UIView *view = [UIView new];
[self.view addSubview:view];
我发现这个问题是在我使用UITabBarController的时候出现的
使 UIScrollView 成为全局变量。像这样加载视图时,使 UIScrollView 的 contentInset 和 UIScrollView 的指示符 UIEdgeInsetsZero:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}