如何限制视图控制器在ios 8中的旋转?
问题描述:
我想禁用几个视图控制器的iOS设备的自动旋转。就像我有一个控制器,我希望它只能以纵向模式显示。在其他视图控制器在横向模式。我使用了下面的代码,但这些代表方法永远不会被调用?如何限制视图控制器在ios 8中的旋转?
#pragma mark Orientation handling
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
答
我用下面的方法来强制设置纵向只对一些选定的观点和我的工作,可能是它会帮助你
1,创建一个全局标志
在你的AppDelegate方法中的全局标志上创建所以你可以在任何视图控制器中访问它。
在你AppDelegate.h
@property (assign) BOOL flagOrientationAll;
在你AppDelegate.m
@synthesize flagOrientationAll;
添加按照您的AppDelegate.m方法
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if(flagOrientationAll == YES){
return UIInterfaceOrientationMaskAll; // All orientation support
} else {
return UIInterfaceOrientationMaskPortrait; // Set only one orientation you want
}
}
2,添加以下代码您要限制旋转的ViewController .m
// set flag "flagOrientationAll" to rotate only one view in your particular view
#import AppDelegate.h
-(void)viewWillAppear:(BOOL)animated
{
NSLog (@"webViewController -- viewWillAppear");
[super viewWillAppear:animated];
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.flagOrientationAll = NO;
}
这里我的帖子:How to set one of the screens in landscape mode in iphone?
如果你得到任何麻烦让我知道!
希望这可以帮助你http://stackoverflow.com/questions/31158690/force-the-ios-device-to-change-orientation/31158872#31158872 – Sujania
你是否在一个导航控制器? 如果是这样,你应该重写导航控制器中的方法。 – Jaeger
我不在导航控制器内 – Techiee