仅允许在一个视图控制器上进行自动旋转 [英] Allow autorotation on just one view controller
问题描述
在我的项目中,我只允许纵向旋转,但对于一个 ViewController
我想启用横向。我将 ViewController
呈现为 ModalViewController
,我尝试使用方法 - (BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
或iOS 6方法,如 - (NSUInteger)supportedInterfaceOrientations
但实际上没有任何效果。虽然这些方法被调用,但视图没有旋转。
In my project I have allowed only portrait rotation, but for one ViewController
I would like to enable also landscape. I'm presenting this ViewController
as ModalViewController
, I've tried using methods - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
or iOS 6 methods like -(NSUInteger) supportedInterfaceOrientations
but nothing actually worked. The view didn't rotate although those methods got called.
在此之后,我试图通过听取这些通知的方式将其旋转:
After this I've tried to rotate it by myslef with listening to those notifications :
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
但即使我能够手动旋转视图
in方法 didRotate
:它非常混乱,我无法旋转 StatusBar
。
but even though I was able to manually rotate the view
in method didRotate
: it's very messy and I can't rotate the StatusBar
.
我真的很想使用像 shouldAutorotateToInterfaceOrientation
这样的标准方法,但我不知道怎么做。任何人?
I would really like to use standard methods like shouldAutorotateToInterfaceOrientation
, but I don't know how. Anyone?
推荐答案
在你的app app.m中添加这个
Add this in your app delegate.m
# pragma mark - Rotation
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController isKindOfClass:[MVYSideMenuController class]]) {
// Get topmost/visible view controller
UIViewController *currentViewController = [self.window.rootViewController.childViewControllers lastObject];
// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
// Unlock landscape view orientations for this view controller
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}
-(void)canRotate
{
}
然后添加此方法
-(void)canRotate
{
// just define the method, no code required here
}
在每个ViewController(.m文件)中你想提供轮换。你也可以在这里包含 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
方法在设备旋转时做出反应:
in every ViewController (.m files) where you want to provide rotation. You can also include here -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
method to react when the device rotates:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case 1:
case 2:
//NSLog(@"portrait");
break;
case 3:
case 4:
//NSLog(@"landscape");
break;
default:
//NSLog(@"other");
break;
}
}
这篇关于仅允许在一个视图控制器上进行自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!