Search code examples
iosiphoneswiftxcodedevice-orientation

How to keep device orientation for iPhone SE exclusively to portrait only?


I'm building in landscape support for my app, but I don't want to support the small iPhone SE screen, which is too difficult to adapt the landscape UI for.

Is there a way to have portrait only for iPhone SE, while all other iPhone models support landscape?

I've tried this code, but doesn't work for me.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if  UIDevice.current.modelName == "iPhone 4"  ||
            UIDevice.current.modelName == "iPhone 5"  ||
            UIDevice.current.modelName == "iPhone 4s" ||
            UIDevice.current.modelName == "iPhone 5s" {

            return .portrait
        } else {
            return .all
        }
    }

Thanks


Solution

  • Never base layout decisions on devices. Base your decisions on screen/view sizes.

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        let size = UIScreen.main.bounds.size
        let len = max(size.height, size.width)
        if len <= 480 {
            return .portrait
        } else {
            return .all
        }
    }