0

Is there any way to allow landscape mode in only one view controller in an app? I'm presenting it modally like so:

let recViewController = AVPlayerViewController()
recViewController.modalTransitionStyle = .CoverVertical
recViewController.player = AVPlayer(URL: NSURL(string: currentScores[selectedButtonIndexPath.row].recapAvailable))
recViewController.player.play()

self.view.window?.rootViewController?.presentViewController(recViewController, animated: true, completion: nil)

I know I can manually override each view controller to only allow for vertical orientation (with the exception of the above one) but that seems rather tedious.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Raymond Rangel
  • 149
  • 3
  • 12

1 Answers1

0

Is there any way to allow landscape mode in only one view controller in an app?

You have answered the question yourself, see the 'Configuring the View Rotation Settings' in UIViewController class reference. You need to configure your app to support all of the rotations you want, then override each view controller, and yes it is tedious, but it works :)

Handling View Rotations As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransitionToSize:withTransitionCoordinator: method. When the interface orientation changes, UIKit calls this method on the window’s root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.

In iOS 6 and iOS 7, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported. The intersection of the app's orientation mask and the view controller's orientation mask is used to determine which orientations a view controller can be rotated into.

You can override the preferredInterfaceOrientationForPresentation for a view controller that is intended to be presented full screen in a specific orientation.

When a rotation occurs for a visible view controller, the willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are called during the rotation. The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.


EDIT

You can also look at the UIApplicationDelegate Protocol, it has the method

func application(application: UIApplication,
supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask

From the documentation:

Discussion

This method returns the total set of interface orientations supported by the app. When determining whether to rotate a particular view controller, the orientations returned by this method are intersected with the orientations supported by the root view controller or topmost presented view controller. The app and view controller must agree before the rotation is allowed.

If you do not implement this method, the app uses the values in the UIInterfaceOrientation key of the app’s Info.plist as the default interface orientations.


Community
  • 1
  • 1
Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44