OWolf
OWolf

Reputation: 5122

Why is self.view height and width different than a sub view's height and width?

What is the logic behind the fact the main screen and self.view always maintain the same width and height dimensions regardless of orientation (even as the self.view does re-size correctly to fit the current device orientation), while a sub-View of self.view returns modified width and height dimensions when the device orientation changes?

NSLog(@"screen bounds w: %f, h: %f", [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height );

NSLog(@"self.view frame w: %f, h: %f", self.view.frame.size.width, self.view.frame.size.height );

NSLog(@"myView frame w: %f, h: %f", _myView.frame.size.width, _myView.frame.size.height );

Log output portrait

2012-11-05 16:15:11.152 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:15:11.153 test[2807:11303] self.view frame w: 320.000000, h: 460.000000
2012-11-05 16:15:11.153 test[2807:11303] myView frame w: 320.000000, h: 460.000000

Log output landscape

2012-11-05 16:14:38.800 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] self.view frame w: 300.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] myView frame w: 480.000000, h: 300.000000

Upvotes: 0

Views: 2801

Answers (3)

Paresh Masani
Paresh Masani

Reputation: 7504

That depends on what autoSizingMask property set for your subviews.

I presume that you are not using AutoLayout feature of iOS 6.0. If you do then you need to look more into Constraints settings in iOS 6.0.

By default your view's autosizing mask is "Scale to Fill" see attached image. You can set same for your subviews based on your requirements. See more on Struts and Springs in Apple docs.

If autosizing doesn't suit your requirements then you will need to implement your custom methods to layout your views on orientation changed. Something like below;

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [UIView animateWithDuration:duration animations:^{
        [self layoutForOrientation:toInterfaceOrientation];
    }];
}

#pragma mark - Layout views

- (void)layoutForOrientation:(UIInterfaceOrientation)orientation {
    if (UIInterfaceOrientationIsPortrait(orientation))
        [self layoutPortraitViews];
    else
        [self layoutLandscapViews];
}

- (void)layoutLandscapViews {
    // Layout your landscape views
}

- (void)layoutPortraitViews {
   // Layout your portrait views
}

default autosizing mask for main view

Upvotes: 1

EugeneK
EugeneK

Reputation: 100

[UIScreen mainScreen] is always 320w and 480h (568h for iPhone5) for iPhone.
Dimensions of [UIScreen mainScreen] is being taken for portrait orientation.

The device status bar is visible in your app as per your NSLog's. Due to this you have 300h in landscape and 460h in portrait.

Upvotes: 1

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

Actually it more than auto-resizing masks. So when you change the orientation of the device, the physical characteristics of the screen remains the same. If you set your view to be auto rotate, the width and height are translated for you. So when asking for screen dimensions with translated views, its imp. for you to do calculations based on the self.view.center or self.view.bounds rather than on self.view.frame.

Upvotes: 1

Related Questions