Search code examples
iphonerotationuiinterfaceorientation

iphone: tap button will rotate view


I have a problem when trying to manually rotate a view. For example I have a view controller running in landscape, and I have a button on that view. I would like to tap that button to force the view rotate to portrait (upside down portrait). I search but can not find the answer.

Thank for your help


Solution

  • You could try this:

    - (void)buttonPressed {
    
        // check current orientation
        if ([[UIApplication sharedApplication] statusBarOrientation] != UIDeviceOrientationLandscapeLeft) {
    
            // no, the orientation is wrong, we must rotate the UI
            self.view.userInteractionEnabled = NO;
    
            // setup status bar
            [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
            // rotate main view, in this sample the view of navigation controller is the root view in main window
            [self.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
            // set size of view
            [self.view setFrame:CGRectMake(0, 0, 748, 1024)];
    
        }
    
    }