xcode 版本4.5 模拟器:6.0
项目需求:刚进去界面横屏,从这个界面进去的界面全是竖屏。
程序的根控制器用了UINavigationController。下面是代码:
1.在appdelegate中添加代码:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
returnUIInterfaceOrientationMaskAll;
}
2.给 UINavigationController添加个category,在实现文件中添加如下代码:
-(BOOL)shouldAutorotate
{
return [[selftopViewController] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[selftopViewController] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[selftopViewController] preferredInterfaceOrientationForPresentation];
}
3.在需要横屏的地方添加(竖屏可以更换方向):
-(NSUInteger)supportedInterfaceOrientations{
returnUIInterfaceOrientationMaskLandscapeRight; // 可以修改为任何方向
}
-(BOOL)shouldAutorotate{
returnYES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
4.最重要的是:页面之间切换不能用导航控制器切换,必须使用:
[selfpresentViewController:navigationController animated:YEScompletion:Nil];