How to rotate the parent view controller when changing the orientation of the child view controller in iOS

I show the Child view controller on the parent view controller using

childView1 *viewController = [[childView1 alloc]initWithNibName:examTFVC_NIB bundle:nil row:gesture.view.tag productID:test_productID size:testsize Duration:duration]; self.modalPresentationStyle = UIModalPresentationCurrentContext; self.view.alpha = 0.4f; viewController.view.backgroundColor = [UIColor clearColor]; viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentViewController:viewController animated:YES completion:NULL]; 

I show a transparent view controller above the parent view controller and its landscape. but the problem is when I change the device from landscape from left to landscape from right or right to left, than the orientation changes of the controller of my child view, but the controller of the parent view remains the same orientation.

My orientation of the parent view controller changes when I do not show the child view controller, how can I change the orientation of the parent view controller along with the child?

One thing I tried to change orientation was by programming the Notification Center transfer from child to parent to change the orientation.

In the child view controller

 - (NSUInteger)supportedInterfaceOrientations { NSUInteger supportedOrientations = UIInterfaceOrientationMaskLandscape; [[NSNotificationCenter defaultCenter] postNotificationName:@"RotateParent"object:nil]; return supportedOrientations; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ); } - (BOOL)shouldAutorotate { return YES; } 

In the parent view controller

 -(void)rotateParent:(NSNotification *)notification { CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI); self.view.transform = transform; NSLog(@"Parent rotate "); } 

but when I click to display my child above the parent view, my rotateParent method executes by default once. any other solution?

+3
ios iphone ios5 ipad uiinterfaceorientation
source share
2 answers

iOS 6.0

in childView1

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { [[NSNotificationCenter defaultCenter] postNotificationName:@"RotateParent"object:nil]; return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight ); } 

For> iOS 6.0

  - (BOOL)shouldAutorotate { [[NSNotificationCenter defaultCenter] postNotificationName:@"RotateParent" object:nil]; return YES; } 

in parent view

Add an observer for notification and, based on the current orientation, rotate the parent view at the right angle.

 -(void)rotateParent:(NSNotification *)note{ UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; CGFloat rotationAngle = 0; if (orientation == UIDeviceOrientationPortraitUpsideDown) rotationAngle = M_PI; else if (orientation == UIDeviceOrientationLandscapeLeft) rotationAngle = M_PI_2; else if (orientation == UIDeviceOrientationLandscapeRight) rotationAngle = -M_PI_2; [UIView animateWithDuration:0.5 animations:^{ self.view.transform = CGAffineTransformMakeRotation(rotationAngle); self.view.transform = CGAffineTransformMakeRotation(rotationAngle); self.view.transform = CGAffineTransformMakeRotation(rotationAngle); } completion:nil]; //adjust view frame based on screen size if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); } else { self.view.bounds = CGRectMake(0.0, 0.0, 320, 480); } } 

let me know if you have any problems.

+3
source share

Hey, if I understood your problem correctly, you can look at this excellent method from UIView .

 - (void)viewWillLayoutSubviews 

When resizing views, the view adjusts the position of its subspecies. Your view controller can override this method to make changes before the view outlines its subclauses. By default, the implementation of this method does nothing.

Thus, this method will be called before each subsequent rotation of your Child View Controller, and you can send a notification to your parent view controller from this method.

0
source share

All Articles