0

I have a viewController and I have

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

why I read w and h as 320 and 480 on its viewDidLoad for width and height, instead of 480 and 320, respectively, when iPhone is landscape? (see below)

- (void) viewDidLoad {
CGFloat w = self.view.bounds.size.width; // 320???
CGFloat h = self.view.bounds.size.height; // 480 ??
}

What am I missing?

thanks.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
Duck
  • 34,902
  • 47
  • 248
  • 470

3 Answers3

2

Did you try to obtain the values in viewDidAppear? With viewDidLoad the view was not actually rendered and so it might be still in portrait.

Andy S.
  • 533
  • 2
  • 6
  • Did you remember to define it as - (void)viewDidAppear:(BOOL)animated? Without the argument it's a different selector, so won't get the call. – Tommy Mar 28 '11 at 18:58
  • If the ViewController is in a tabbar controller or navigation controller, viewDidAppear may or may not get called, container controllers are not helpful when it comes to viewDidAppear type methods and rotation – Jonathan. Mar 28 '11 at 19:50
2

Be careful, it is possible you have run in to perhaps the most famous bug on the platform:

iPhone app in landscape mode, 2008 systems

This is a bug that catches a lot of people. Notice that on that question, one of the responses has 22 votes - that response is actually completely, totally, 100% wrong.

That shows some of the confusion on the issue.

Be sure to check out everything on there and all the links, eg:

"An important reminder of the ADDITIONAL well-known problem at hand here: if you are trying to swap between MORE THAN ONE view (all landscape), IT SIMPLY DOES NOT WORK. It is essential to remember this or you will waste days on the problem. It is literally NOT POSSIBLE. It is the biggest open, known, bug on the iOS platform."

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • thanks. The big question is why Apple don't fix that!!! I am banging my head on the wall for hours! – Duck Mar 29 '11 at 03:54
  • I'm sure I don't understand the link you sent me, but I've just made an iPad app that runs completely in landscape mode. Ok, its annoying to have to set all should autorotates to only landscape, however its not impossible... :/ In fact it was relatively simple... – Thomas Clayson Mar 29 '11 at 15:44
1

I always have this problem...

I think that all views must render in portrait mode and then switch to landscape. Its a bitch, but the best way to manage this is to use UIViewAutoreizingMasks on all sub-views, that way they'll layout how you want them to.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • This matches my experience, though I don't see how it's a bitch. Even if you're doing everything programmatically then just be sure to set the correct portrait/landscape positions in your willAnimateRotationToInterfaceOrientation:duration: and everything will just work. No need to add your own stuff to duplicate the landscape/portrait test in viewDidLoad. – Tommy Mar 28 '11 at 18:23
  • 2
    no the bitch bit comes when your app DOESN'T rotate, its just in landscape mode. I'm making an iPad app thats just in landscape mode all the time, and its difficult to set things against the size of the view. For instance (the worst one, because you can't use autoresizing masks) the contentSize of a UIScrollView. – Thomas Clayson Mar 28 '11 at 18:26
  • 1
    yes, that's my problem. My app is not all landscape, but if I start it while iPhone is landscape and don't change that, it will fail. This is an annoying problem!!! – Duck Mar 28 '11 at 18:39
  • Admitted, those two things do sound like a major hassle. I've never been in the position of writing a landscape app and setting up any aspect of the view programmatically, so combine that with a lack of imagination and it's clear I've overlooked something rather than come to a different conclusion about how hard it is. – Tommy Mar 28 '11 at 19:00
  • Digital robot - the problem lies in that you can't have a "choose per page" app. This is what I've found. If SOME of your app is landscape ALL of your app has to be landscape. The only way around this is to use modal view controllers rather than pushing them onto a nav controller. However this is very hacky and usually ends up with an odd looking app (some views sliding from the bottom, some from the side). If some of your views are landscape, then all of them should be (or in other words, each of your views should have a landscape and portrait view). – Thomas Clayson Mar 28 '11 at 19:43
  • It's a bitch because sometimes it is correct and sometimes it isn't. You'd think Apple would have sorted this by now. – Jonathan. Mar 28 '11 at 19:47