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

4

2 回答 2

3

对于 < 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;
  }

在父视图中

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

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

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

于 2013-07-24T16:30:29.037 回答
0

嘿,如果我正确理解了您的问题,那么您可能想从UIView.

- (void)viewWillLayoutSubviews

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

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

于 2013-07-24T13:46:09.017 回答