UIViewController do not rotate to landscape

2019-03-03 22:34发布

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.

In that ViewController I have this:

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

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.

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.

How to deal with this controller to force Portait ?

Please read the bolded text too: can't force the whole app to support only Portait for 1 view controller, there are many on stack!

2条回答
三岁会撩人
2楼-- · 2019-03-03 22:56

Try marking the app's supported Interface orientations in the properties file to only being portrait. But then of course in that function you just return YES on view controllers that you want to allow rotation. But then when you push it back in the stack the other views should be portrait.

portrait only orientations

查看更多
爷的心禁止访问
3楼-- · 2019-03-03 23:13

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.

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

查看更多
登录 后发表回答