【问题标题】:How can I rotate a UIViewController to its supported interface orientation when navigating to it?导航到 UIViewController 时,如何将其旋转到其支持的界面方向?
【发布时间】:2016-04-11 20:30:32
【问题描述】:

我有一个锁定为纵向的视图控制器(带有子视图控制器),使用以下代码来完成此操作:

- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
    if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.centerViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskAll;
}

这非常有效,如果我需要锁定该视图,我可以“覆盖”当前 centerViewController 中的 supportedInterfaceOrientations,如果我希望该视图支持所有内容,则可以将其排除在外。

问题是锁定的视图在导航时不会旋转到其支持的方向。一个视图被锁定为纵向,但是当以横向显示另一个视图并导航到该视图时,它以横向显示,即使此视图不支持横向。

如何确保在导航时将视图旋转到允许的方向?

【问题讨论】:

    标签: ios cocoa-touch ios8 ios9


    【解决方案1】:

    如果您在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) {
        }
    }
    

    【讨论】:

    • 我已在supportedInterfaceOrientations 中返回了我支持的方向,但是当从另一个方向的另一个视图导航到视图时,视图不会自动旋转。
    • 我刚刚在我的应用程序中尝试了上面的代码,试一试。
    • 我认为这是因为我实际上并没有导航。我有一个 mainViewController,它有一个 childViewController。导航时将删除子级,并由新的子级视图控制器替换。主视图控制器的生命周期方法不会以这种方式触发。我能以某种方式强迫他们这样做吗?
    • 不。我不认为这是可能的。
    【解决方案2】:

    AppDelegate.h中添加以下属性

    @property (readwrite) BOOL restrictRotation;
    

    AppDelegate.m中添加如下代码

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if(self.restrictRotation)
            return UIInterfaceOrientationMaskLandscape;
        else
            return UIInterfaceOrientationMaskPortrait;
    }
    

    在要限制方向的 ViewController.m 中添加以下代码:

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

    当你离开你的 Landscape ViewController.m 时,请进行以下函数调用

    [self restrictRotation:NO];
    

    希望我能解决你的问题。

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多