3

I'm having some real issues understanding why center on my views behave like it does, totally discarding orientation of the device (and my otherwise good google-skills give nothing)... I understand how frame, bounds and center relate to one another/superviews etc. (here is a good explaination).

So, I have a few ViewControllers (call them A & B). B is presented "modally" by A, using a "Cover Vertical"-transition (really not that relevant, but who knows...).

Now, running the following code

self.view.center = CGPointMake(self.view.frame.size.width * 0.5f, self.view.frame.size.height * 0.5f + 100);

inside any of the viewcontrollers (triggered by a button-press), will move the view in the wrong direction for all orientations except portrait (portrait "up"). Basically Portrait will move the view down 100px (Yes!), Landscape will move either left or right 100px, and Upside Down will move up 100px. Looking at the device in hand it will move the view by 100px from the same physical side for each orientation (for a visual reference).

Am I missing something very obvious here, using some weird settings or is this just the expected behaviour? (I hope it's the first or second one).

In my mind, x is always horizontally and y is always vertically for all orientations, and that this is managed at a lower level (by the OS).

So, please point out the obvious and have a good laugh at my expense! :)

Edit.

Okey, so I started an empty project (with a single view), and the behaviour was the same. But then I dragged in a Navigation Controller in IB, setting the main-controller (A) as root. Simsalabim and yes it behaves as I want it to... Ofc I can remove the navigation-bar and it looks the same. I know the "window" and root-viewcontroller work together to setup orientation (viewcontroller determining if it's allowed etc.), but is this the way to go? Why doesn't it work with a single ViewController? Or do you just expect UIView.center to behave this "oddly" if your initial ViewController is just a plain "View Controller"? Or do you always have a navigationController as the initial point? Brain... Hurts...

Edit 2.

Okay, so there seems to be ZERO consistency on when the frame/bounds is returning what. Basically, if you have a simple ViewController as the initial controller (e.g. not embedded as root-viewcontroller in a NavigationController), the orientation will be wrong. It will be visually correct, but frame ignores orientation and bounds seem to be correct most of the time. When would you ever want the frame to ignore the actual orientation (how the view/gui is rendered)?! Of course I want it to be like I see it on the device! This makes custom segues a awful pain, since, say, a sliding animation won't look the same in landscape and portrait... I've seen multiple examples in blogs etc. "explaining" how it works, but none actually work in both orientations... Do people just accept this nonesense or is it just me actually seeing the issue with it? I've googled like a maniac and NO ONE has had a working example, all just hacking and slashing til it "seem to work"...

^Rage :)

Community
  • 1
  • 1
helmesjo
  • 625
  • 5
  • 17
  • Have you verified your view controllers are actually rotating when you change the orientation? – Teddy Nov 06 '13 at 17:13
  • Yes! Been printing both bounds, frame and center to verify, and they are changing accordingly. – helmesjo Nov 06 '13 at 21:45
  • Just to clearify: They change accordingly IF I use a navigation controller (keeping **A** as root-VC). If a just set **A** as the initial VC it will visually rotate correctly, but the frame and bounds live their own lives... – helmesjo Nov 08 '13 at 08:33

1 Answers1

0

Okey, so after some heavy digging I thought I might answer this as best I can for any others having the same headache!

To sum up what I've read (and how I've interpreted it, being somewhat new to iOS):

Basically, using a sole View Controller as initial VC will place the View directly under the main window, which just as the screen of the device never actually change orientation (width & hight of the screen is always the same physical sizes, no matter orientation). This is why it works as expected when you put the same View Controller as root in a Navigation Controller, since the Navigation View will now server as the superview for all other Views in the hierarchy (properly recalculating the transform in different orientations).

For me this was totally not obvious, which caused the big confusion! I took it for granted that this was handled by the OS because I just can't imagine when it would be useful to have an oblique axis... But that's just me :) Anyways, hope this helps atleast some people!

Here is a good explaination: UIViewController returns invalid frame?

Community
  • 1
  • 1
helmesjo
  • 625
  • 5
  • 17