OGeek|极客世界-中国程序员成长平台

标题: iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:17
标题: iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转

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

我的 UINavigationController 子类:

@interface RotationViewController : UINavigationController

@end

@implementation RotationViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

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

    return UIInterfaceOrientationMaskPortrait;
}

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

    return UIInterfaceOrientationPortrait;
}

@end

我只想要横向的 View Controller :

@implementation LoadingViewController

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

@end

其他 View Controller 不实现这些方法中的任何一个。



Best Answer-推荐答案


我也有同样的情况。所以你可以在你的应用程序中实现下面的代码

- (void)viewDidAppearBOOL)animated
{ 
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].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)viewWillDisappearBOOL)animated
{
    [super viewWillDisappear:animated];   

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].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)

注意:-

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

你不需要实现

- (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

View Controller 中的函数....所以删除这些方法....

关于iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127645/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4