简体   繁体   中英

Change view controller on device rotation

I am working on my first Xcode project and I am wondering if it is possible to have 2 view controllers, one which will have a portrait view and the second that will have horizontal. Is there any way I can link these 2 view controllers up so that when the device is rotated the view will change?

In viewDidLoad, add the rotated view to the superview and register an observer to UIDeviceOrientationDidChangeNotification with an object:[UIDevice currentDevice]. On the selector set its frame. Then rotate the new rotated view, and set the old view's alpha to 0.0

- (void)deviceOrientationChangedNotification:(NSNotification *)notification 
{
     UIDevice *device = [notification object];
     UIDeviceOrientation orientation = device.orientation;

     if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) 
     {
        [rotatedView setTransform:CGAffineTransformMakeRotation(M_PI_2)];
        rotatedView.frame = [UIScreen mainScreen].bounds;
        oldView.alpha = 0.0;
     }
}

I don't recommend you to do that, because there are some intuitives UI controllers that apple has designed to manage the navigation of the app, you can't let the users to guess how to do some tasks in your app, you have to show them exactly how to do it. So, rotate the device to go to another view controller, as you said :I want a completely different view when it is rotated... Is not a good idea.

Read the complete iOS Human Interface Guidelines to see what's recommended and what's not:

iOS Human Interface Guidelines

In particular, read: Adaptivity and Layout :

Avoid gratuitous changes in layout. A comparable experience in all environments lets people maintain their usage patterns when they rotate a device or run your app on a different device. For example, if you use a grid to display images in a horizontally regular environment, you don't have to display the same information in a list in a horizontally compact environment, although you might adjust the dimensions of the grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM