-2

I have many View Controllers in my ios App and they all support rotation but for one of my scenes (and only one), I would like to detect the trait environment and run it only if the device trait environment is regular height (so an iPhone in portrait or an iPad in both portrait or landscape). How to achieve this? So I want to get the vertical size class and based on which I want to freeze the orientation. So if verticalSizeClass == .compact, the orientation of views of the UIViewController should be portrait else the orientation of the views of the UIViewController can be the same as that of the screen. I am using the following code

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    let tc = UIScreen.main.traitCollection
    let orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation

    if tc.verticalSizeClass == .compact {
        return .portrait
    } else {
        return orientation
    }

}

But it's giving error messages for autolayout. I am using Xcode 10.2, running Swift 5.0. There is a question How to force view controller orientation in iOS 8? but it is from objective c perspective and more importantly I want the viewController to rotate as per the device, while the question "How to force view controller orientation in iOS 8?" locks the orientation to portrait or landscape and does not care about landscape left or right.

Nikhil Pandey
  • 262
  • 4
  • 11
  • So you want to force portrait if the screen is vertically compact, and support all orientations if otherwise? – Sweeper Apr 02 '19 at 06:46
  • @Sweeper exactly this is what I want. – Nikhil Pandey Apr 02 '19 at 06:47
  • Possible duplicate of [How to force view controller orientation in iOS 8?](https://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8) – Aks Apr 02 '19 at 07:12
  • @Aks The question I have asked is slightly different as for regular height environment, I want the viewController to rotate as per the device, while the question "How to force view controller orientation in iOS 8?" locks the orientation to portrait or landscape and does not care about landscape left or right. – Nikhil Pandey Apr 02 '19 at 07:19
  • iOS has size classes, not devices for auto layout. So that answer will apply to your case also. We identify via size class `Compact, Regular, Any` device can vary. And iPad multitasking can also vary this. – Aks Apr 02 '19 at 07:22
  • Btw one answer from that question also include different `UIInterfaceOrientationMask` like `landscapeleft` and `landscaperight`. Maybe try out the answers and then come back if still have issues. – Aks Apr 02 '19 at 07:30
  • @Aks please point out to that answer, I searched but couldn't find and that's why asked this question. – Nikhil Pandey Apr 02 '19 at 07:32

2 Answers2

1

You have correctly used the size class to detect the trait environment. But try overriding this stored property of UIViewController instead.

override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get{
    if .compact == UIScreen.main.traitCollection.verticalSizeClass{
        return .portrait
    }
    return .all
}

}

When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns true.

Reference Documentation

Prateek Pande
  • 495
  • 3
  • 12
0

as I understand you want to get the height of specific Scene let screenSize: CGRect = UIScreen.main.bounds

    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    print("Screen width = \(screenWidth), screen height = \(screenHeight)")

this gives you the height and the width.

Eman Shedeed
  • 114
  • 2
  • 9
  • No, I want to get the vertical size class and based on which I want to freeze the orientation. So if verticalSizeClass == .compact, the orientation of views of the UIViewController should be portrait else the orientation of the views of the UIViewController can be the same as that of the screen. – Nikhil Pandey Apr 02 '19 at 06:39
  • if (UIScreen.main.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.compact) { } else { } and if you need to check for vertical if (UIScreen.main.traitCollection.verticalSizeClass == UIUserInterfaceSizeClass.compact){}else{} – Eman Shedeed Apr 02 '19 at 09:56