如何在iOS中的子视图控制器方向更改时旋转父视图控制器

我在我的父视图控制器上使用显示子视图控制器

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]; 

我显示透明的视图控制器,而不是我的父视图控制器和其景观。 但问题是当我从横向更改设备从风景左到风景右或从右到左比我的子视图控制器的方向更改,但父视图控制器保持相同的方向。

我的父视图控制器的方向更改时,我不显示子视图控制器如何更改父视图控制器的方向随着孩子?

有一件事我试图改变方向通过编程传递通知中心从孩子到家长改变方向。

在子视图控制器中

 - (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; } 

在父视图控制器中

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

但是当我点击显示我的孩子在父视图比我的“rotateParent”方法得到执行一次默认情况下。 其他解决scheme?

对于<iOS 6.0

在childView1中

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

对于> iOS 6.0

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

在父视图中

添加通知观察者,并根据当前的方向以适当的angular度旋转父视图。

 -(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); } } 

让我知道你是否面临任何问题。

嘿,如果我已经正确地理解你的问题,比你可能想要从UIView这个超级方法。

 - (void)viewWillLayoutSubviews 

视图边界发生变化时,视图会调整子视图的位置。 你的视图控制器可以覆盖这个方法来进行更改之前,视图布局它的子视图。 这个方法的默认实现什么也不做。

所以这个方法会在你的子视图控制器的每一个后续的旋转之前被调用,并且你可以通过这个方法向你的父视图控制器发布通知。