Search code examples
iphoneviewrotationlandscape

Working with Landscape Views designed with interface builder


I'm trying to understand something that , by googling around, seems like a bug in the iPhone sdk, but maybe its just something hidden im not sure of.

I designed a new view in Interface Builder, in landscape mode. It looks like this:

But when i load it in from one of my other views, even though it is landscape, all the content doesnt look as it looks inside interface builder, it looks like this:

I found some code in google that helped to sort-of fix this issue, but the solution isn't perfect , and honestly its just kind of an ugly workaround, the code is (in viewDidLoad):

// First rotate the screen:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;

// Then rotate the view and re-align it:
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation( 1.570796327 ); // 90 degrees in radian
[self.view setTransform:landscapeTransform];

This code actually almost completely solves the problem, but still doesn't look exactly like my XIB:

I'd love to know if any of you creative minds have a better solution for this, and if not, if you can help me change the code above to maybe come closer to the "original" design in the xib file.

Thanks in advance! :)
Shai.


Solution

  • The reason your view is being pushed up is because the it's starting in the left top corner behind the status bar. Try adding this after the rotation:

    landscapeTransform = CGAffineTransformMakeTranslation(0, 20);
    [self.view setTransform:landscapeTransform];