sebastien FCT
sebastien FCT

Reputation: 630

CGRectMake( ) - y reference looks totally wrong

I am facing a really strange issue:

I am instantiating multiple UIImageView inside a for loop with the method CGRectMake, the y origin I am giving seems to be totally wrong on the screen:

Here is my code:

- (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];
    }
}

According to this code avatar is within header at 5px from the top of header.

But the following is what I obtain visually:

enter image description here

note: when the white area begin, the header view stopped

This is not a real issue since I can reevaluate my frames like this :

CGRectMake(5 + i * 75, - 20, 70, 70)

but it looks really weird, and I am quite sure I am missing something totally trivial here...

Upvotes: 1

Views: 158

Answers (2)

Sulthan
Sulthan

Reputation: 130200

I think this will be fixed by:

self.automaticallyAdjustsScrollViewInsets = NO;

Since iOS 7, view controllers automatically adjust scroll view insets so that the scroll view content is not hidden behind the navigation bar because it expects scroll views to start at the top of the screen.

However, the usual solution is to just set the scrollview frame.origin.y to 0.

Upvotes: 2

Hemant Chittora
Hemant Chittora

Reputation: 3162

Your Code is Absolutely Correct , As you are Adding the scrollview on (0,64) Position , So 64 will be count from Bottom of the Navigation Bar, If you want it on top (Just Below the Navigation bar), Change this declaration to as below :

UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)]; 

Upvotes: 0

Related Questions