4

I want to lock single viewcontroller in iPhone and iPad. This below code is working perfectly in iPhone 4,5,6 iPad, iPad 2 ,iPad retina. But not working in iPad pro.

@implementation UINavigationController (Orientation)
-(NSUInteger)supportedInterfaceOrientations
{
        return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
@end

This above code is written in my view controller which view controller i do not want to rotate.

Monika Patel
  • 2,287
  • 3
  • 20
  • 45

2 Answers2

3

Write this below code in view controller, which view controller u want to lock in portrait mode

@implementation UINavigationController (Orientation)
-(NSUInteger)supportedInterfaceOrientations
{
        return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
@end

#pragma mark Orientation
-(BOOL)shouldAutorotate
{
    [super shouldAutorotate];
    return NO;
}
-(NSUInteger) supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    [super preferredInterfaceOrientationForPresentation];
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

And now do this below changes in your plist file enter image description here

Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • Thank you! I thought I had tried everything. Set the right checkboxes on project info, checked full screen, overriding the right methods. They were even being called. I fixed the problem by going into info.plist and removing the orientations for iPad. Silly that they are not displayed on project info for a universal project. – VaporwareWolf Jul 13 '17 at 02:57
1

Write this in your view controller which you don't want to rotate

This will prevent any rotation.

The view controller class you don't want to rotate should have this.

- (BOOL)shouldAutorotate
{
    return NO;
}

The containing navigation controller class should have this.

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutoRotate];
}

This will only rotate to portrait

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Chetan
  • 2,004
  • 1
  • 13
  • 21
  • Don't declare the both methods in view controller. One is to be declared in view Controller and one in navigation controller class. Read the answer again – Chetan Mar 22 '16 at 05:32