Eric
Eric

Reputation: 117

UIView size getting screwed when launching in Landscape

I have a containment UIViewController and in my viewDidLoad method I am loading its child view controller from the storyboard. Then I add the child view controllers view as a sub view and try to set this new views size:

  childVC.view.frame=CGRectMake(0,0,768,1004);

What happens is that when you start the app in Landscape mode, you get a 20px "gap" at the left side (bottom side of the physical device). I can avoid this if I get the device orientation in my code and use

  CGRectMake(0,0,748,1024);

for the frame property instead when landscape is detected. However, I think there must be a different solution.

Before the question comes up: I set the frame instead of the bounds property because once this problem is solved I need to position the view somewhere else than 0,0. As far as I know there's no other way to do this, right?

Upvotes: 1

Views: 736

Answers (1)

rmaddy
rmaddy

Reputation: 318934

Set the child view's frame based on the parent view's bounds. Then be sure that the child view's autoresizing mask is set to both "flexible width" and "flexible height". Don't hardcode sizes like you are.

childVC.view.frame = parentVC.view.bounds;
childVC.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Once this works, you can tweak the child view's frame as needed. But it still should be based on the parent view's bounds.

Upvotes: 4

Related Questions