how to rotate parent view controller when child view controller orientation changes in iOS
Asked Answered
B

2

3

I am displaying Child view controller on my 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 am displaying transparent view controller over my parent view controller and its landscape only. but issue is when i change the device from landscape left to landscape right or right to left than my child view controller orientation changes but parent view controller remain same orientation.

My parent view controller orientation changes when i am not displaying child view controller how can i change parent view controller orientation along with child?

One thing i tried to change orientation by programming passing Notification Center from child to parent to change the orientation.

In 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 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 over parent view than my "rotateParent" method get executed one time by default. any other solution??

Bracteole answered 24/7, 2013 at 13:14 Comment(4)
So you are not been able to pass the notification on subsequent rotations right?Sydel
can you try sending notification from shouldAutorotateToInterfaceOrientationTgroup
@Tgroup this will not work..because it supported only upto ios 5 ..and i am testing on ios 6Bracteole
FYI -- you're not using the correct terminology. Presenting a controller does not make that controller a child. You get a parent-child relationship when you use addChildViewController:, or when you use container views in a storyboard. The correct terms for your scenario are presented and presenting view controllers.Analog
T
3

For < 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 observer for notification, and based on current orientation rotate parent view with proper 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 face any issue.

Tgroup answered 24/7, 2013 at 16:30 Comment(2)
I change your answer according to my need and it works on both iOS 6 and less than 6Bracteole
i used CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(90*M_PI/180.0);Bracteole
S
0

Hey if I have understood your problem correctly, than you may want to look into this superb method from UIView.

- (void)viewWillLayoutSubviews

When a view’s bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

So this method will get called before every subsequent rotation of your Child View Controller and you can post notification to your parent view controller from this method.

Sydel answered 24/7, 2013 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.