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??
但是当我点击在父视图上显示我的孩子时,我的“rotateParent”方法在默认情况下会被执行一次。其他的解决方案?
2 个解决方案
#1
3
For < iOS 6.0
< iOS 6.0
in childView1
在childView1
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[NSNotificationCenter defaultCenter] postNotificationName:@"RotateParent"object:nil];
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
For > iOS 6.0
> 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.
如果你面临任何问题,请告诉我。
#2
0
Hey if I have understood your problem correctly, than you may want to look into this superb method from UIView
.
嘿,如果我正确地理解了你的问题,你可能不想从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.
这个方法在子视图控制器每次旋转之前都会被调用你可以通过这个方法向父视图控制器发出通知。
#1
3
For < iOS 6.0
< iOS 6.0
in childView1
在childView1
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[NSNotificationCenter defaultCenter] postNotificationName:@"RotateParent"object:nil];
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
For > iOS 6.0
> 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.
如果你面临任何问题,请告诉我。
#2
0
Hey if I have understood your problem correctly, than you may want to look into this superb method from UIView
.
嘿,如果我正确地理解了你的问题,你可能不想从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.
这个方法在子视图控制器每次旋转之前都会被调用你可以通过这个方法向父视图控制器发出通知。