0

I want to say in each view controller which orientation he supports and restrict it to them. This is what I tried:

None of the solutions seems to work. Or do I miss something important? How can I restrict the orientation that view controller A is landscape only, view controller B is portrait only and view controller C can be shown in all available orientations?

Community
  • 1
  • 1
testing
  • 19,681
  • 50
  • 236
  • 417

1 Answers1

1

Overriding GetSupportedInterfaceOrientations() in your view controller should solve the problem:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations ()     {
  return UIInterfaceOrientationMask.LandscapeRight;
}

The orientations you return here will be intersected with the app's allowed orientations (project properties) or with what you allow in your app delegate. So be sure to specify all orientations there or the maximum set of orientations you want to support.

See here for documentation.

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Thanks a ton! I removed the override for `ShouldAutorotate()` in my view controllers and now it works. I'm using my [*CustomNavigationController*](http://stackoverflow.com/a/26012695/426227). One more question though: If I navigate from view controller A (only landscape) to view controller B (only portrait) then the orientation doesn't update. I'm in landscape despite view controller B only allows portrait. Only after rotation the behavior is correct again. – testing Sep 21 '15 at 13:25
  • How do you navigate from A to B? `UINavigationController` push? – Krumelur Sep 21 '15 at 14:33
  • Yes `NavigationController.PushViewController (testViewController, true);` – testing Sep 21 '15 at 14:47
  • Have you tried using a plain `UINavigationController` to see if your custom one creates the problem? – Krumelur Sep 21 '15 at 15:15
  • If I use the default `UINavigationController` then my orientations are not restricted. This does only work if the view controller is the current top most view controller (so no navigation controller used at all). That's the reason why I used my custom navigation controller, which queries the top most view controller if it should rotate or not. – testing Sep 22 '15 at 08:08
  • Do you had some time looking into this issue? – testing Sep 24 '15 at 10:38
  • I'm not sure what else to try. The solution I gave you should not require any changes on the navigation controller. – Krumelur Sep 24 '15 at 11:37
  • Would it help if I provide a sample project to reproduce this issue? – testing Sep 24 '15 at 11:38
  • 1
    Not really. I just made a demo project and I see now what your issue is. Here's a similar question: http://stackoverflow.com/questions/6064900/how-do-i-force-a-specific-uiinterfaceorientation-on-an-individual-view-in-a-uina - my thinking is that it is not good design to have one view controller that will enforce rotation. – Krumelur Sep 24 '15 at 12:05
  • Thanks for your help. I agree with you on that point. My application would look nicer if one could force the orientation. Now the system has to calculate the layout twice. I also think Apple is doing that in one of their applications (calendar on iPhone). Now I'll try to do it without enforcement and perhaps that will be enough. – testing Sep 24 '15 at 12:17