菜鸟教程小白 发表于 2022-12-12 09:17:58

iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转


                                            <p><p>我有一个 <code>UINavigationController</code> 堆栈中的所有 <code>UIViewController</code> 都是纵向的,除了最后一个是横向的。我当前的实现在推送时以纵向显示最后一个 ViewController ,但我可以旋转到横向,并且不能旋转回纵向。 <strong>如何在 View 被推送时强制旋转为横向?</strong></p>

<p>我的 <code>UINavigationController</code> 子类:</p>

<pre><code>@interface RotationViewController : UINavigationController

@end

@implementation RotationViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.topViewController.class == )
    {
      return UIInterfaceOrientationMaskLandscape;
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if (self.topViewController.class == )
    {
      return UIInterfaceOrientationLandscapeLeft;
    }

    return UIInterfaceOrientationPortrait;
}

@end
</code></pre>

<p>我只想要横向的 ViewController :</p>

<pre><code>@implementation LoadingViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

@end
</code></pre>

<p>其他 ViewController 不实现这些方法中的任何一个。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我也有同样的情况。所以你可以在你的应用程序中实现下面的代码</p>

<pre><code>- (void)viewDidAppear:(BOOL)animated
{
    ;

    [ setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 480, 44.0);
}

-(void)viewWillDisappear:(BOOL)animated
{
    ;   

    [ setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [.view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320, 480);
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 320.0, 44.0);
}

#define degreesToRadian(X) (M_PI * (X)/180.0)
</code></pre>

<p>注意:-</p>

<p><strong>以上代码是针对iphone实现的。因此,如果您将其用于 ipad,请更改边界。</strong></p>

<p>你不需要实现</p>

<pre><code>- (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
</code></pre>

<p> ViewController 中的函数....所以删除这些方法....</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15127645/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15127645/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转