如何在导航控制器中自动旋转子视图
问题描述:
我在导航控制器中有一个子视图,当iPad模拟器旋转为纵向或横向时,主视图是自动旋转适合iPad的方向,但子视图不是。子视图也旋转,但不适合ipad方向,子视图方向总是朝着ipad主页按钮。如何在导航控制器中自动旋转子视图
我的问题是,如何使子视图自动旋转并具有像主视图的方向?
有些身体可以帮助我,请...
答
我已经解决了这个问题。
我使用NSNotification功能的子视图的viewController类,在viewWillAppear中的方法,这是我实现:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
,然后,这是deviceRotated方法:
-(void)deviceRotated:(id)sender{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
if (orientation == UIDeviceOrientationPortrait) {
CGAffineTransform rotate = CGAffineTransformMakeRotation (0.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 30);
self.alertView.transform = CGAffineTransformConcat(translate, rotate);
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
CGAffineTransform rotate = CGAffineTransformMakeRotation (M_PI * 180/180.0f);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-63, -100);
self.alertView.transform = CGAffineTransformConcat(translate, rotate);
}else if (orientation == UIDeviceOrientationLandscapeLeft) {
CGAffineTransform rotate = CGAffineTransformMakeRotation (M_PI * 90/180.0f);
CGAffineTransform translate = CGAffineTransformMakeTranslation(60, -180);
self.alertView.transform = CGAffineTransformConcat(translate, rotate);
}else if (orientation == UIDeviceOrientationLandscapeRight) {
CGAffineTransform rotate = CGAffineTransformMakeRotation (M_PI * 270/180.0f);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-60, -140);
self.alertView.transform = CGAffineTransformConcat(translate, rotate);
}
}
最后,问题已经解决了。 this是帮助我的链接。