Monika Patel
Monika Patel

Reputation: 2375

Disable autorotate on a single UIViewController not working in iPad Pro

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.

Upvotes: 4

Views: 5571

Answers (2)

Monika Patel
Monika Patel

Reputation: 2375

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

Upvotes: 3

Chetan
Chetan

Reputation: 2124

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;
}

Upvotes: 1

Related Questions