如何在iOS中禁用特定的方向

我想在某些视图中禁用横向。

我已经覆盖了以下两种方法,但是这些方法不会随时调用。

- (NSUInteger) supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } 

我错过了什么吗? 请帮帮我。

创buildUINavigationController的子类并按照下面的方式覆盖这些方法:

 - (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; } 

并在你想设置纵向方向的UIViewController类中使用下面的方法

 - (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; } 

看看这个答案。 这个人可能已经详细解释了代表。

另外尝试,

 - (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; } } 

你是否在你的代码中使用了下面的方法,而这个supportedInterfaceOrientations不会被调用

– (BOOL)shouldAutorotate {

 return YES; 

}

Interesting Posts