5

在我的项目中,我只允许纵向旋转,但对于一个ViewController我想启用横向。我将其呈现ViewControllerModalViewController,我尝试过使用方法- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation或 iOS 6 方法,-(NSUInteger) supportedInterfaceOrientations但实际上没有任何效果。尽管调用了这些方法,但视图没有旋转。

在此之后,我尝试通过 myslef 旋转它并收听这些通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

但即使我能够手动旋转viewin 方法didRotate:它非常混乱,我无法旋转StatusBar.

我真的很想使用标准方法shouldAutorotateToInterfaceOrientation,但我不知道如何。任何人?

4

4 回答 4

6

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
{
}

and then add this method

-(void)canRotate
{
    // just define the method, no code required here
}

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;
    }
}
于 2014-07-15T08:34:00.853 回答
2

为需要旋转的屏幕子类化导航控制器。

在他们中

// Older versions of iOS (deprecated) if supporting iOS < 5
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation    {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

// iOS6
 - (BOOL)shouldAutorotate {
return YES;
 }

 // iOS6
 - (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}

这将覆盖在 iOS 6 的摘要页面中设置的旋转方法。

在 iOS 6 中,视图控制器只查看父控制器或根控制器的旋转方法

于 2012-12-26T14:18:01.143 回答
0

你不能像这样在 viewDidLoad 和 viewWillAppear 中调用 shouldAutoRotateToInterfaceOrientation:

[self shouldAutorotateToInterfaceOrientation];

如果它在您的 ViewController 中,则应该调用该方法

于 2014-04-08T21:22:26.103 回答
-1

在此处输入图像描述

实现在所有控制器中,并在特定控制器所需的 interfaceOrientation 上返回

对所有人

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{

 return YES;
}

景观

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

肖像

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
于 2012-12-26T12:39:37.240 回答