2

the below method is not called when i rotate my device with Autolayout ON in IB.

willRotateToInterfaceOrientation:duration:

Firstly does the above methods don't wok with autolayout? the method gets called with Autolayout turned off. I am trying to change the background Image of my view on rotation, Not sure how to achieve that with autolayout constaints ON?

I also tried to use procedure by Moin but I have a huge view hierarchy and the methods are called from superview onwards, and and i have lot of other code in other methods like. So it didn't help

didRotateFromInterfaceOrientation  
Community
  • 1
  • 1
Irfanlone
  • 564
  • 7
  • 16
  • 2
    You must be doing something incorrectly, because that method running shouldn't have anything to do with whether auto layout is on or off. I always use auto layout, and that method works for me. – rdelmar Jul 12 '14 at 01:50
  • @rdelmar : thanks. So the methods get called automatically when i rotate the device. Only when I change my xib to autlayout they dont get called. I tested this on all the xib files and the methods didnt get called. So what could be possibly going wrong? because it works fine without autolayout. Is there something I am missing ? – Irfanlone Jul 12 '14 at 02:53
  • Sorry, I have no idea. It's always worked for me. I just tried it on a new app with a xib defined controller, and it was called. – rdelmar Jul 12 '14 at 03:00
  • What's the root controller? A `UITabBarController`, a `UINavigationController` or neither ? – SwiftArchitect Jul 13 '14 at 04:51

2 Answers2

1

The short answer: -willRotateToInterfaceOrientation:duration: works with AutoLayout.

The long answer: Assuming you are using NIB, so many compounding issues can go wrong ; I have compiled this checklist:

  1. Did you set your custom UIViewController in IB as recommended by @JoshJustice at Loaded nib but the view outlet was not set - new to InterfaceBuilder
  2. Are you properly handling auto-rotation as recommended by @nybon at UIViewController does not auto rotate
  3. Did you set a global orientation in the plist as recommended by @NateFlink at Handling autorotation for one view controller in iOS7
  4. Does your custom UIViewController respond to -supportedInterfaceOrientations with incorrect values? See recommended response by @AndrewHershberger at preferredInterfaceOrientationForPresentation must return a supported interface orientation (iOS 6)
Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Thanks for your answer. Can you provide more detail on what did you meant by "Did you set your custom UIViewController in IB" and "Are you properly handling auto-rotation?" as I said in my question, auto rotation works perfectly without autolayout. – Irfanlone Jul 12 '14 at 03:00
  • There is no one answer for why it went wrong in your app (*until you find it that is*), but there is an answer to your question: *Firstly does the above methods work with autolayout?* **yes, they do** I have formatted my response and added links to the likely places you should look into. – SwiftArchitect Jul 12 '14 at 05:51
1

The -willRotateToInterfaceOrientation:duration: gets called on the root viewcontroller and all its child viewcontrollers and works with autolayout. If you want these methods to get called on a particular viewcontroller, It must be child viewcontroller of the root viewcontroller.

It took me a while to figure out that the hierarchy from the root to the last viewcontroller was breaking at some point. I just had to add it back as the child view controller.

Irfanlone
  • 564
  • 7
  • 16