我正在使用以下方法在父视图控制器上显示子视图控制器
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”方法一次。还有其他解决方案吗?
对于<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);
}
}
让我知道您是否遇到任何问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句