views:

645

answers:

3

I have a UINavigationController that can rotate to landscape mode and that it initializes a new UIViewController and pushes it on the stack. On the UIViewController's viewDidLoad I just print the self.view.frame.size.

If the Navigation Controller is in Portrait mode, the View Controller will print {320, 460}. However, when the Navigation Controller is in Landscape, the frame size is exactly the same. (To clarify, in the second case the UIViewController is initialized while the phone is already in landscape mode.)

Shouldn't the frame size of the view be rotated? And if not how I can I find the correct frame size without hardcoding the numbers?

A: 

You can't rely on the frame in landscape mode; you have to use a combination of bounds and center; frame is generated using a combination of those, and when there's a non-identity transform applied (as there is in landscape), it gets a little weird.

Ben Gottlieb
Can you please elaborate? The bounds are also identical {320, 460} in both cases and the center remains unchanged too.
If you're asking why the frame is not re-sized, are your springs-and-struts set properly? That's the first thing to check.
Ben Gottlieb
Yes and my view resizes fine when it is rotated. The only issue is when it is created while on Landscape.
Are you saying that when you create it in landscape mode, it's ending up with a different bounds from what's being passed to initWithFrame?
Ben Gottlieb
What I'm saying is that the view is always created in portrait mode and I can't find a way to get the correct frame size programmatically without hardcoding it.
What frame are you passing it when creating? Can you post some source?
Ben Gottlieb
The problem is that I don't know what frame to pass. If I let IB create the view (use initWithNib) it is always in portrait. If I try to create the view programmatically, I don't know what frame to pass it (apart from hardcoding the numbers).
A: 

Hi, Petsos (and Ben),

Did you ever get this figured out? I have been trying to get a navigation controller to look normal in landscape mode. I had been using a transform to rotate it, and using frame to position it, and it would never come out even close to normal-looking.

I've tried a million ways, and I'm still looking for code that someone has used that truly works. Kind of tired of experimenting, and I'm looking for actual usable code!

Thanks, /Steve

+1  A: 

Hi

first you need to set your view to resize automatically with a proper autoresizingMask. with this your view will adapt to the size of the controller itself.

you can check this yourself with an NSLog. But don't put it in loadView, this is too early. Put this in viewWillAppear.

If you set the autoresizingMask of your view with Interface Builder you should turn off the Simulated Interface Elements in the Attributes inspector. If any of these is on you can't change the autoresizingMask in the Size inspector.

denis2342