• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 6 – 强制 UINavigationController 内的 UIViewController 旋转

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:17:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap