Abdullah Shafique Abdullah Shafique - 3 months ago 19
iOS Question

supportedInterfaceOrientations not working

I want some of my ViewControllers landscape and some portrait so this is what I did:

I enabled landscape mode:

enter image description here

Next I added these lines of code to the view controllers I wanted to be Portrait:

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
-(BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}


However when I rotate them they still go to landscape mode.How can I fix that?

Answer

Here is how I do it.
Make a UINavigationController parent class.
inside you UINavigationController(parent) override these methods this way:

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

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

    }
    return NO;
}

Now your NavigationController should be a subclass of the is UINavigationContorller