how to rotate parent view controller when child vi

2019-04-11 00:49发布

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??

2条回答
劫难
2楼-- · 2019-04-11 01:00

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.

查看更多
别忘想泡老子
3楼-- · 2019-04-11 01:00

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.

查看更多
登录 后发表回答