UIViewController do not rotate to landscape

后端 未结 2 1077
时光说笑
时光说笑 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!

    0 讨论(0)
  • 2021-01-28 23: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

    0 讨论(0)
提交回复
热议问题