views:

228

answers:

3

Hi,

I have a small UIView that should appear always in the center of the screen. This works great for portrait mode but does not in landscape.

To draw the UIView I use:

myView.frame = CGRectMake((visibleArea.size.width - MY_VIEW_WIDTH) / 2, (visibleArea.size.height - MY_VIEW_HEIGHT) / 2, MY_VIEW_WIDTH, MY_VIEW_HEIGHT);
myView.autoresizingMask = (
                            UIViewAutoresizingFlexibleTopMargin |
                            UIViewAutoresizingFlexibleBottomMargin |
                            UIViewAutoresizingFlexibleLeftMargin |
                            UIViewAutoresizingFlexibleRightMargin
                           );

Any ideas what might be missing?

A: 

Maybe you don't change the orientation of the status bar. If you don't do this, the device thinks that the orientation still portrait, not landscape.

Espuz
I did not touch the status bar and it actually changes position when I rotate the interface.The thing is that no matter in what mode the interface is you always get the bounds of portrait mode when asking for [[UIScreen mainScreen] bounds]. This is obviously no useful when the user starts the app in landscape. Other then that the autoresizing seems to be confused by something.
bresc
Mmmm, try to get the frame with [[UIScreen mainScreen] applicationFrame]. I'm not sure if this frame changes with the orientation but, if don't do it, get the device orientation and, if it's in landscape, switch the width and height.
Espuz
it's the same with frame
bresc
A: 

Why not try

myView.center = controller.view.center

RVN
A: 

Is the app auto rotating?

I mean, what does your -[UIViewController shouldAutorotateToInterfaceOrientation:] method looks like? Do you have an initial and supported orientations in your Info.plist file?

Check also parent's view autoresizesSubviews property.

madmw