UIViewController does not rotate into terrain

In many situations, it is necessary to turn the controller and not work. Right now, I have the inverse problem: it rotates, and I want to disconnect.

In this ViewController, I have the following:

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

but it automatically rotates because this UIViewController is not being requested, but its parent in the user interface tree. Perhaps this is the problem. The root controller should return Yes for all cases, because there are several other UIViewControllers on the stack that have / must have Portait / Landscape support.

I can’t / don’t want to touch other parts, because ... there are several reasons, for example: the application is huge, with a lot of errors, and I don’t want to do 1 and check it for 1 week, the other is the deadline.

Please do not suggest that this is not so and should be rewritten. I know.

How to deal with this controller to force Portait?

Also read the bold text: it cannot force the entire application to support only Portait for 1 view controller, there are a lot of them on the stack!

0
source share
2 answers

determine the turn of the terrain and turn on 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)];
        }
    } 
}

this is not the best programming paradigm, but it does the trick.

- , , , !

+1

, . , , YES , . , , .

portrait only orientations

+2

Source: https://habr.com/ru/post/1704033/


All Articles