krisdyson
krisdyson

Reputation: 3255

Setting the correct UIView frame size on alloc/init depending on device orientation

I have set up all my views in portrait mode, then set the autoresize mask accordingly so that it will auto resize the views as the orientation changes.... however, this seems to work OK for views that are already displayed. If I load any new views whilst in landscape mode I get inverted width and height values on the loaded view's frame.

Do I have to calculate the frame size based on orientation for each of the main views? e.g., instead of

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,500, 100)];

I would have to do:

CGRect frame;
if([UIDevice orientation] == UIDeviceOrientationLandscape){
   frame = CGRectMake(0,0,100,500)
}else{
    frame = CGRectMake(0,0,500,100)
}
UIView* view = [[UIView alloc] initWithFrame:frame];

It seems a bit long-winded to me, given that the autorotation once a view is loaded works perfectly!

How do you manage view frames?

thanks

Upvotes: 1

Views: 4981

Answers (1)

Zebs
Zebs

Reputation: 5448

I manage my view frames using [[UIScreen mainScreen] applicationFrame];.

It is also very useful when building universal apps as you can use it to design views that will display correctly on both devices.

Upvotes: 2

Related Questions