Allowing only one view of several in a tab bar setup to rotate

727 views Asked by At

I have a tab bar controller with four different views. Recently, I wanted to incorporate rotating for only one of my views. I read somewhere that for tab controllers, all subviews needed to return YES in the method shouldAutoRotateToInterfaceOrientation:. I have done this and everything works, however, I only want one of the views to go into landscape mode, not all. I can only seem to get it in an all-or-nothing situation, meaning its either all rotate or none.

1

There are 1 answers

1
Totumus Maximus On BEST ANSWER

Add this to those VCs that only support portrait mode:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (interfaceOrientation == UIDeviceOrientationPortrait ||  interfaceOrientation ==  UIDeviceOrientationPortraitUpsideDown) 
    {
        return YES;
    }
    return NO;
}

Add this to the VC that supports landscape too:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return YES;
}

The above would mean you need 4 VC's to control the tabs which isn't what you wanted. What you can do as well is make an extra boolean in your mainVC which is NO by default. Then when you open that particular view that supports all orientations you just put that boolean on YES and do the following code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if (myBoolThatAllowsAllOrientations)
    {
        return YES;
    }
    if (interfaceOrientation == UIDeviceOrientationPortrait ||  interfaceOrientation ==  UIDeviceOrientationPortraitUpsideDown) 
    {
        return YES;
    }
    return NO;
}