UIViewController do not rotate to landscape

后端 未结 2 1104
时光说笑
时光说笑 2021-01-28 23:30

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 ViewContr

2条回答
  •  青春惊慌失措
    2021-01-28 23:49

    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!

提交回复
热议问题