UIViewController不会旋转到横向 [英] UIViewController do not rotate to landscape

查看:59
本文介绍了UIViewController不会旋转到横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多情况下需要旋转控制器并且无法正常工作。
现在我有问题的反转:它正在旋转,我想禁用。

In many situation need to rotate the controller and is not working. Right now I have the inverse of the problem: it is rotating, and I want to disable.

在那个ViewController中我有这个:

In that ViewController I have this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

但它是自动旋转的,因为不是这个UIViewController被问到,但他的UI树中的父级。也许这就是问题所在。 根控制器必须为所有情况返回Yes,因为堆栈上还有一些其他UIViewControllers,它们必须具有/必须具有Portait / Landscape支持。

but it is auto-rotating, because not this UIViewController is asked, but his parent in UI tree. Maybe that is the problem. The root controller must return Yes for all cases, because there are a few other UIViewControllers on the stack, which has / must have Portait / Landscape support.

我不能/不想触摸其他部分,因为...有几个原因,例如:应用程序很大,有很多已知错误,我不想做1并测试它持续1周,其他是截止日期。

I can't / don't want to touch other parts, because ... there are several reasons, for eg: the app is huge, with lot of know bugs and I don't want to make 1 and test it for 1 week, other is the deadline.

请不要暗示它不应该是这样,必须重写。我知道。

Please don't suggest it shouldn't be like this and must rewritten. I know.

如何处理这个控制器以强制使用Portait?

How to deal with this controller to force Portait ?

请阅读粗体文字: 无法强制整个应用程序仅支持1个视图控制器的Portait,堆栈中有很多!

推荐答案

检测Landscape旋转并旋转到Portait:

detect the Landscape rotation and rotate to Portait:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{ 

    UIInterfaceOrientation appOrientation = [UIApplication sharedApplication].statusBarOrientation;
    float width = self.view.bounds.size.width;
    float height = self.view.bounds.size.height;
    //NSLog(@"width %3.0f, height: %3.0f", width, height);

    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){
        // if is rotated from Portait:
        if((appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight)){
            // to Landscape:
            CGAffineTransform transform = self.view.transform;
            transform = CGAffineTransformRotate(transform, -(M_PI / 2.0));
            self.view.transform = transform;            

            [self.view setBounds:CGRectMake(0, 0, height, width)];
        }
    }
    else {
        // it is rotated from Landscape:
        if((appOrientation == UIInterfaceOrientationPortrait || appOrientation == UIInterfaceOrientationPortraitUpsideDown)){
            // to Portrait:            
            CGAffineTransform transform = self.view.transform;
            transform = CGAffineTransformRotate(transform, +(M_PI / 2.0));
            self.view.transform = transform;            

            [self.view setBounds:CGRectMake(0, 0, height, width)];
        }
    } 
}

它不是最好的编程范式,但它确实可以解决问题。

it isn't the best programming paradigm, but it does the trick.

如果可以的话,有人会写类似tis来接受他的回答,或者写一个更好的方法!

Somebody write similar like tis to accept his answer, or write a better method, if you can!

这篇关于UIViewController不会旋转到横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆