0

I have a rotation (orientation change) issue in an iOS app. The app has several view controllers to navigate.

I want it to be possible to rotate except for iPhone 4 (3.5 inches devices). On a 3.5 inches device it shoulld always stay in portrait mode.

I am able to prevent direct rotation, that is when inside a view controller, if the user holds the device in portrait mode an them in landscape mode nothing happens. This is exactly what I want.

Problems happen when changing view controller while holding in landscape mode. In this case the new view controller shows up in landscape mode, and I don't want that.

I tried to use

- (BOOL)shouldAutorotate;

returning NO when the device is 3.5 inches but that does not work.

How can I prevent this behavior?

I may need to add one detail: the 3.5 inches device I am using to test runs iOS version 9.3.5.

I am using Xcode 9.4.1.

Tushar Katyal
  • 412
  • 5
  • 12
Michel
  • 10,303
  • 17
  • 82
  • 179

2 Answers2

1

You can use the method supportedInterfaceOrientations.

For example:

Objective c

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //return your preferred orientation 
    return UIInterfaceOrientationMaskPortrait;
}
Michel
  • 10,303
  • 17
  • 82
  • 179
Enrique Bermúdez
  • 1,740
  • 2
  • 11
  • 25
  • To be precise and exact I needed to make a slight fix, but you gave me the way to go. I fixed your code so it works if other people look at this post. – Michel Sep 04 '18 at 17:32
0

You need add this code in AppDelegate.m. iPhone 4/ iPhone 4s has screen height of 480 points.

Objective-C

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
    if(self.window.bounds.size.height > 500) {
        return (UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait);
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

You may also need to enable Require Full Screen in Target's Deployment Info.

Sunny
  • 821
  • 6
  • 17