我遇到了一个非常奇怪的问题:
我在 for 循环
中使用 CGRectMake
方法实例化多个 UIImageView
,我给出的 y 原点似乎完全错误屏幕:
这是我的代码:
- (void)makeTheView
{
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];
header.backgroundColor = [UIColor colorWithRed:254/255.0f green:255/255.0f blue:213/255.0f alpha:1.0f];
[self.view addSubview:header];
for (int i = 0; i < 10; i++) {
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(5 + i * 75, 5, 70, 70)];
avatar.image = [UIImage imageNamed:@"bo_pic_baby5.jpg"];
[avatar.layer setCornerRadius:8.0];
avatar.layer.masksToBounds = YES;
NSLog(@"%f", avatar.frame.origin.y);
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, avatar.frame.size.width, 20)];
title.backgroundColor = [UIColor colorWithRed:148/255.0f green:148/255.0f blue:148/255.0f alpha:0.5f];
title.font = [UIFont fontWithName:@"Arial" size:15];
title.text = @"崔健";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[avatar addSubview:title];
[header addSubview:avatar];
}
}
根据此代码,avatar
位于 header
内,距 header
顶部 5px。
但以下是我在视觉上获得的:
注意:当白色区域开始时,header
View 停止
这不是真正的问题,因为我可以像这样重新评估我的框架:
CGRectMake(5 + i * 75, - 20, 70, 70)
但它看起来真的很奇怪,我很确定我在这里遗漏了一些完全微不足道的东西......
最佳答案
我认为这将通过以下方式解决:
self.automaticallyAdjustsScrollViewInsets = NO;
自 iOS 7 起, View Controller 自动调整 ScrollView 插入,以便 ScrollView 内容不会隐藏在导航栏后面,因为它希望 ScrollView 从屏幕顶部开始。
但是,通常的解决方案是将 ScrollView frame.origin.y
设置为 0
。
关于ios - CGRectMake( ) - y 引用看起来完全错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24652665/