只有一个视图在 xcode 中自动旋转?

Posted

技术标签:

【中文标题】只有一个视图在 xcode 中自动旋转?【英文标题】:Only having one view autorotate in xcode? 【发布时间】:2011-11-07 22:04:19 【问题描述】:

好的,所以我目前有 3 个视图,我只需要其中一个来自动旋转到任何方向,而其余的则保持纵向。现在我的设置是一个splashviewcontroller淡入视图A,而视图A内部是一个切换到视图B的按钮。我想要的是视图B能够旋转到任何方向。

当我在 splashviewcontroller 中为 shouldautorotatetointerfaceorientation 返回 YES 时,每个视图都会旋转,因为这是父视图。当我仅在启动视图中返回肖像时,即使其他视图返回 YES,也不会旋转。有没有办法只让视图 B 旋转?如果您可以提供代码,我愿意手动完成。谢谢

【问题讨论】:

B是怎么加的?如果它不是独立的(即通过导航控制器),这可能就是问题所在。 @stavash 那么我该如何让它独立呢?我通过添加 UIViewController 子类来添加 @stavash 有没有办法在没有标签栏的情况下制作独立视图?我可以使用按钮链接到独立视图吗? 【参考方案1】:

您可以像这样手动管理任何所需 UIView 对象的旋转:

编辑:

在 init 或 viewDidLoad 中

- (void)viewDidLoad

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];

    [super viewDidLoad];


#define degreesToRadian(x) (M_PI * (x) / 180.0)

-(void)rotate

    self.view.bounds = CGRectMake(0, 0, 0, 0);

    if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait)


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];


     else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown)


        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);

        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);

        [self.view setTransform:landscapeTransform];

     else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight)

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));

         landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
         self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 


        [self.view setTransform:landscapeTransform];

    else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)

        CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));

        landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
        self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 

        [self.view setTransform:landscapeTransform];
    


【讨论】:

当我把它放在我的 ViewB.m 中时没有任何反应。视图 B 是一个模态视图控制器,我可能做错了什么愚蠢的事情吗? 为了让它工作,你必须在捕捉到旋转事件后触发旋转方法。请检查答案中的编辑 好的,这样可以完美运行,但问题是当视图旋转时,我有一个视频在 ViewB 内的 webview 内播放,我也需要旋转。有什么建议可以做到这一点吗?谢谢【参考方案2】:

在目标摘要部分,Supported Interface Orientation 所有项目都被选中,除了 Upside Down,所以你需要做的就是转到处理视图的 .m 文件并使用这段代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

这对我有用。

【讨论】:

以上是关于只有一个视图在 xcode 中自动旋转?的主要内容,如果未能解决你的问题,请参考以下文章

Xcode 自动布局和旋转

在视图控制器之间切换时自动旋转

如何在一个视图不应该自动旋转而一个视图应该自动旋转的 iOS 应用程序中处理自动旋转?

iPad 应用程序中的旋转

在屏幕旋转之前,自动布局约束不正确? [复制]

旋转Xcode时不同的xib文件