0

I am struggling to get my UISplitViewController to always stay in landscape mode. The mothods that work with other ViewControllers don't seem to work here.

In my AppDelegate I have:

var supportedInterfaceOrientation: UIInterfaceOrientationMask = .all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return supportedInterfaceOrientation
    }

In my UISplitViewController I have:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
override func viewDidLoad() {
    super.viewDidLoad()
    appDelegate.supportedInterfaceOrientation = .landscapeLeft
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}

deinit {
    appDelegate.supportedInterfaceOrientation = .all
}

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}

And I am presenting my UISplitViewController like this:

let vc = self.storyBoard.instantiateViewController(withIdentifier: "CompareSplitVC") as! CompareSplitViewController

vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: {})

All of this has no effect and one of these are being called: shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

What am I doing wrong?

Edit: I am getting this issue on iPad but not on iPhone.

Tilman
  • 734
  • 5
  • 10
  • Two things, both likely dumb on my part. (1) The first thing that caught my eye was you are using UISplitViewController - something I'm polishing up right now. You do know that in iOS/iPadOS 14 there's also a compact view controller when that is the size class, right? I just don't think you should be trying to do what you are.... (2) Here's the dumb question. If I wanted something to be landscape only, I'd try to uncheck portrait in the Target/General window in Xcode. Have you tried that? –  May 09 '21 at 01:26
  • I am hijacking UISplitViewController to display two view controllers at the same time. I want them to be side by side in landscape and on top of each other in portrait mode. The app supports both orientations. I thought it would be elegant to use UISplitViewController for this since it has a lot of the functionality that I am looking for built in. But it seems that on iPad we cannot restrict interface orientation for view controllers. I might have to go for a UICollectionView instead. – Tilman May 09 '21 at 15:00
  • I see. First, beware that `UISplitViewController` uses size classes - and on all iPads in full screen mode (meaning not multi-tasking, the size class is regular in all orientations. For iPhones, it's different. An iPhone 12 mini, iPhone 8, iPhone 11, iPhone 11 Pro Max, and iPhone 12 Pro Max it will *always* show the compact VC - even in landscape.... –  May 09 '21 at 15:45
  • I have code that uses auto layout constraints and view controller methods to give two different layouts depending on orientation. It displays an image with sliders below in portrait and sliders to the right in landscape. There's no storyboard, just a `UIViewController` with `UIViews`, `UILabels` and `UISliders` as subviews. If you think this code will help, I'd be happy to post it. –  May 09 '21 at 15:46

0 Answers0