Lock orientation regardless of rotation

2019-03-04 18:17发布

问题:

I have a UITabBar application with embedded UINavigation for some of the views. On one specific navigationview I am displaying graphs/charts and it would be better to display them in landscape as if the iPhone was rotated to the left or right. The rest of the app is better suited to portrait. So I want to "force" the views containing graphs to load in landscape regardless of how the user has the device physically rotated. I've tried:

#pragma mark - Rotation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

But this does not seem to do anything to the view. If I select the "Landscape Left" icon in the "Supported Interface Orientations" for my target then it allows the entire app to re-orientate on rotation of the device. Is there a way to lock my app in portrait for all normal views and lock in landscape for my views containing graphs such that the app ignores the actual device orientation?

回答1:

Firstly I am using iOS 6 SDK.

I am using a custom TabBar Controller. and controlling the orientation of that TabBar Controller by the below set of codes

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

And the view controller should contain

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
    return UIInterfaceOrientationIsLandscape(orientation);
}

This is what I did and was able to lock a view to landscape or portrait (in my case). To force may be you can put a alert view stating the user to turn the device to landscape and show him the graph. Just a suggestion.



回答2:

You are right. In tab bar applications it is not the shouldAutorotateToInterfaceOrientation: method of the individual view controllers being called. Only the tab bar controller's shouldAutorotateToInterfaceOrientation is called to determine whether and how the views can be oriented.

However, the individual view controllers should implement shouldAutorotateToInterfaceOrientation accordingly anyway. Those methods are still used to determine the orientation of animation effects when pushing or pulling a view controller.

I never tried the following myself: You could try subclassing the tab bar controller and respond to shouldAutorotateToInterfaceOrientation accordingly depending on which view is currently shown to the user. But I fear that Apple has good reasons for forcing us to support the same orientations for all views within a tab bar app.



回答3:

Try it in particular ViewController in which you need to required in landscape mode:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );

}