Search code examples
iosviewuiviewcontrollerframebounds

View Controller's View's properties


I'm working on the interface for a specialized calculator and I have the following setup in IB: Interface Builder

I used the "Container View" from IB and set it equal to my custom view controller, "ButtonViewController".

Then, inside my ButtonViewController.m file, I have the following:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ];
    NSLog(@"self.view.bounds.size.width:\t%f", self.view.bounds.size.width);
    NSLog(@"self.view.bounds.size.height:\t%f", self.view.bounds.size.height);

    NSLog(@"self.view.frame.size.width:\t%f", self.view.frame.size.width);
    NSLog(@"self.view.frame.size.height:\t%f", self.view.frame.size.height);
}

I get the following screen output:

Screenshot

And the following console output:

self.view.bounds.size.width:    320.000000
self.view.bounds.size.height:   460.000000
self.view.frame.size.width:     320.000000
self.view.frame.size.height:    460.000000

Interface Builder states that the "container" is 280px wide and 364px high, so for some reason, I'm not getting the correct values.

I even tried to find super's bounds and frame dimensions, but they still came out as 320x460.

Can someone please tell me what I'm doing wrong?

Thanks in advance

[update]

Just some info for others: I've done some testing with similar UI controls and here's what I found: (cvObject is a UICollectionView object)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewWillLayoutSubviews
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

-(void)viewDidLayoutSubviews
{
    NSLog(@"%s: self.cvObject.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.bounds));
    NSLog(@"%s: self.cvObject.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.cvObject.frame));
}

And what I got was:

-[FPViewController viewDidLoad]: self.cvObject.bounds:  {{0, 0}, {0, 0}}
-[FPViewController viewDidLoad]: self.cvObject.frame:   {{0, 0}, {0, 0}}
-[FPViewController viewWillAppear:]: self.cvObject.bounds:  {{0, 0}, {0, 0}}
-[FPViewController viewWillAppear:]: self.cvObject.frame:   {{0, 0}, {0, 0}}
-[FPViewController viewWillLayoutSubviews]: self.cvObject.bounds:   {{0, 0}, {0, 0}}
-[FPViewController viewWillLayoutSubviews]: self.cvObject.frame:    {{0, 0}, {0, 0}}
-[FPViewController viewDidLayoutSubviews]: self.cvObject.bounds:    {{0, 0}, {280, 312}}
-[FPViewController viewDidLayoutSubviews]: self.cvObject.frame: {{20, 128}, {280, 312}}
-[FPViewController viewDidAppear:]: self.cvObject.bounds:   {{0, 0}, {280, 312}}
-[FPViewController viewDidAppear:]: self.cvObject.frame:    {{20, 128}, {280, 312}}

So you can see that it's only after the view lays out its subviews that it determines the frame and bounds of your object.


Solution

  • You should re-examine these values in viewDidAppear and you'll find that you'll see the correct values there:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.view.backgroundColor = [UIColor colorWithRed: 0.75 green: 0.0 blue: 0.0 alpha: 1.0 ];
    
        // frame and bounds are not entirely reliable at this point
    
        NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds));
        NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame));
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        // frame and bounds have generally been updated correctly by this point
    
        NSLog(@"%s: self.view.bounds:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.bounds));
        NSLog(@"%s: self.view.frame:\t%@", __FUNCTION__, NSStringFromCGRect(self.view.frame));
    }