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

北慕城南 提交于 2019-12-04 12:38:54

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!