{
  "$type": "site.standard.document",
  "description": "How to support different device orientations on different screens within the same iOS app, covering changes in iOS 6 and RubyMotion.",
  "path": "/supporting-different-orientation-within-ios-app/",
  "publishedAt": "2013-06-03T06:49:00.000Z",
  "site": "at://did:plc:bryys25pc2fnagnyxqgsglhd/site.standard.publication/3mn26bjkkmh23",
  "tags": [
    "iOS Development",
    "Techniques"
  ],
  "textContent": "I've seen people asking how to support different device orientations within the same iOS app, especially since iOS 6 introduced some changes to how device orientation is handled and with the growing popularity of RubyMotion (I wrote a RubyMotion tutorial for Objective C developers), there is a new breed of developers. It's really straightforward when the entire app supports the same set of orientation(s). But it gets a little confusing when you want different screens to support different orientations.\n\nHere's some sample code for how to do it correctly in RubyMotion. Developers using Objective C should be able to adapt this too.\n\nIn Rakefile (for ObjC developers, do something similar in your info.plist file), set the following to indicate all the orientations supported by the app. If you have a screen in your app that plays video and you want to allow the user to also play it in landscape, you need to include both portrait and landscape orientations here even if the rest of the app is always in portrait.\n\napp.interface_orientations =\n    [:portrait, :landscape_left, :landscape_right]\n\nThe rest of it is code. If you use UINavigationControllers anywhere in your app, you will want to use your own subclass instead:\n\nclass AppDelegate\n  def application(application,\n        didFinishLaunchingWithOptions:launchOptions)\n    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)\n    @window.rootViewController =\n        NavigationController.alloc.initWithRootViewController(\n            PortraitViewController.alloc.init)\n    @window.makeKeyAndVisible\n    true\n  end\nend\n\nThe custom UINavigationController just forwards the relevant calls to it's topmost view controller: class NavigationController < UINavigationController #####Orientation\n\n  def shouldAutorotate\n    topViewController.shouldAutorotate\n  end\n \n  def supportedInterfaceOrientations\n    topViewController.supportedInterfaceOrientations\n  end\n\n  def preferredInterfaceOrientationForPresentation\n    topViewController.preferredInterfaceOrientationForPresentation\n  end\n  \n  def shouldAutorotateToInterfaceOrientation(interfaceOrientation) {\n    topViewController.shouldAutorotateToInterfaceOrientation(\n        interfaceOrientation)\n  end\nend\n\nHere's how to implement a UIViewController subclass that only supports portrait orientation: class PortraitViewController < UIViewController def initWithNibName(nibName, bundle:nibBundle) super(nibName, nibBundle)\n\n    self.view.backgroundColor = UIColor.blueColor\n    b = UIButton.buttonWithType(UIButtonTypeRoundedRect)\n    b.frame = [[60, 100], [200, 20]]\n    b.setTitle('Show VC that rotates', forState:UIControlStateNormal)\n    b.addTarget(self, action:'click',\n        forControlEvents:UIControlEventTouchUpInside)\n    view.addSubview(b)\n    self\n  end\n \n  def click\n    vc = PortraitAndLandscapeViewController.new\n    navigationController.pushViewController(vc, animated:true)\n  end\n \n  #####Orientation\n \n  def shouldAutorotate\n    true\n  end\n \n  def supportedInterfaceOrientations\n    UIInterfaceOrientationMaskPortrait\n  end\n\n  def shouldAutorotateToInterfaceOrientation(interfaceOrientation)\n    interfaceOrientation == UIInterfaceOrientationPortrait\n  end\n\n  #Optional depending on your app\n  def preferredInterfaceOrientationForPresentation\n    UIInterfaceOrientationPortrait\n  end\nend\n\nHere's how to implement a UIViewController subclass that supports both portrait and landscape orientations: class PortraitAndLandscapeViewController < UIViewController def initWithNibName(nibName, bundle:nibBundle) super(nibName, nibBundle) self.view.backgroundColor = UIColor.greenColor\n\n    self\n  end\n \n  #####Orientation\n \n  def shouldAutorotate\n    true\n  end\n \n  def supportedInterfaceOrientations\n    UIInterfaceOrientationMaskAllButUpsideDown\n  end\n\n  def shouldAutorotateToInterfaceOrientation(interfaceOrientation)\n    interfaceOrientation == UIInterfaceOrientationPortrait ||\n    interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||\n    interfaceOrientation == UIInterfaceOrientationLandscapeRight\n  end\n\n  #Optional depending on your app\n  def preferredInterfaceOrientationForPresentation\n    UIInterfaceOrientationPortrait\n  end\nend",
  "title": "Supporting Different Orientations Within an iOS App"
}