【问题标题】:how to rotate parent view controller when child view controller orientation changes in iOS当iOS中的子视图控制器方向发生变化时如何旋转父视图控制器
【发布时间】:2013-07-23 23:52:15
【问题描述】:

我正在使用

在我的父视图控制器上显示子视图控制器
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”方法默认执行一次。 任何其他解决方案?

【问题讨论】:

  • 所以您无法在后续轮换中传递通知,对吧?
  • 你可以尝试从 shouldAutorotateToInterfaceOrientation 发送通知
  • @prasad 这不起作用..因为它只支持到 ios 5 ..我正在测试 ios 6
  • 仅供参考——您使用的术语不正确。呈现控制器并不会使该控制器成为孩子。当您使用 addChildViewController: 或在情节提要中使用容器视图时,您将获得父子关系。为您的场景提供了正确的术语并展示了视图控制器。

标签: iphone ios ipad ios5 uiinterfaceorientation


【解决方案1】:

适用于

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

在父视图中

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

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

如果您遇到任何问题,请告诉我。

【讨论】:

  • 我根据需要更改您的答案,它适用于 iOS 6 和小于 6 的系统
  • 我使用了 CGAffineTransform rotateTransform = CGAffineTransformMakeRotation(90*M_PI/180.0);
【解决方案2】:

嘿,如果我正确理解了您的问题,那么您可能想从UIView 研究这个极好的方法。

- (void)viewWillLayoutSubviews

当视图的边界发生变化时,视图会调整其位置 子视图。您的视图控制器可以覆盖此方法以使 在视图布置其子视图之前更改。默认 这个方法的实现什么都不做。

因此,每次后续轮换子视图控制器之前都会调用此方法,您可以通过此方法向父视图控制器发布通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多