3

I want to disable landscape orientation in some view.

I have overridden the following two methods but these method is not going to call at any time.

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

Am I missing something? Please help me.

mins
  • 6,478
  • 12
  • 56
  • 75
Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59

3 Answers3

2

Create subclass of UINavigationController and override these methods as per below:

- (NSUInteger)supportedInterfaceOrientations
{
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientationsForThisContorller)])
    {
        return(NSInteger)[self.topViewController performSelector:@selector(supportedInterfaceOrientationsForThisContorller) withObject:nil];
    }
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotate
{
    if([self.visibleViewController respondsToSelector:@selector(shouldAutorotateNow)])
    {
        BOOL autoRotate = (BOOL)[self.visibleViewController
performSelector:@selector(shouldAutorotateNow)
                                 withObject:nil];
        return autoRotate;
    }
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

and use below method in your UIViewController class where you want to set only portrait orientation

- (NSUInteger) supportedInterfaceOrientationsForThisContorller {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

-(BOOL)shouldAutorotateNow
{
    return YES;
}
Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59
kiturk3
  • 549
  • 10
  • 30
1

Check out this answer. The guy has probably explained the delegates in much detail.

Also try,

- (void)awakeFromNib
{
    isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                 selector:@selector(orientationChanged:)
                                 name:UIDeviceOrientationDidChangeNotification
                                 object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        isShowingLandscapeView = YES;
    }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             isShowingLandscapeView)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        isShowingLandscapeView = NO;
    }
}
Community
  • 1
  • 1
Kaey
  • 4,615
  • 1
  • 14
  • 18
0

Did you used below method in your code, with out this supportedInterfaceOrientations will not get called

-(BOOL) shouldAutorotate {

return YES; 

}

Nikhil.T
  • 241
  • 2
  • 13
  • Yes that is written by me – Mrugesh Tank Apr 21 '15 at 06:30
  • Well if you are using Nav Controller this must work then, otherwise if this is inside a subview only root will receive this method call. Also check orientation support set properly in info plist. – Nikhil.T Apr 21 '15 at 06:39