问题
I have a viewcontroller (with child viewcontrollers) that is locked in portrait, using the following code to accomplish this:
- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.centerViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAll;
}
This works perfectly, I can "override" the supportedInterfaceOrientations in my current centerViewController if I need to lock that view, and leave it out if I want the view to support everything.
The problem is that the views that are locked don't rotate to their supported orientation upon navigation. One view is locked in portrait, but when showing another view in landscape and navigating to this one shows it in landscape, even though landscape is not supported for this view.
How can I make sure the view is rotated to an allowed orientation upon navigating?
回答1:
I will get rotated automatically if you have returned your supported orientation in supportedInterfaceOrientations
.
#pragma mark - Orientation Handling
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate {// iOS 6 autorotation fix
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {// iOS 6 autorotation fix
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {// iOS 6 autorotation fix
return UIInterfaceOrientationPortrait;
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
}
}
回答2:
Add following property in AppDelegate.h
@property (readwrite) BOOL restrictRotation;
Add following code in AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait;
}
Add following code in ViewController.m which you want to restrict orientation:
-(BOOL)shouldAutorotate
{
return NO;
}
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
- (NSUInteger) supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskLandscape;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// (iOS 6)
// Prefer (force) landscape
return UIInterfaceOrientationLandscapeRight;
}
Also when you leaving from your Landscape ViewController.m make following function call
[self restrictRotation:NO];
Hopefully i solves your problem.
来源:https://stackoverflow.com/questions/34655338/how-can-i-rotate-a-uiviewcontroller-to-its-supported-interface-orientation-when