How to set device (UI) orientation programmatically?

后端 未结 10 1526
感情败类
感情败类 2020-11-30 01:14

Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa.

How do I go about doing this? Is this private?

I

10条回答
  •  再見小時候
    2020-11-30 01:58

    In iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is absent. But we can rotate a view with status bar, for this:

    - (void)showAlbum {
        // check current orientation
        if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) {
            // no, the orientation is wrong, we must rotate the UI
            self.navigationController.view.userInteractionEnabled = NO;
            [UIView beginAnimations:@"newAlbum" context:NULL];
            [UIView setAnimationDelegate:self];
            // when rotation is done, we can add new views, because UI orientation is OK
            [UIView setAnimationDidStopSelector:@selector(addAlbum)];
            // setup status bar
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];
            // rotate main view, in this sample the view of navigation controller is the root view in main window
            [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
            // set size of view
            [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
            [UIView commitAnimations];
        } else {
            [self addAlbum];
        }
    
    }
    

提交回复
热议问题