内容如题。
一般手机应用中很少用到横屏,一般都是视频播放类,或者应用中查看时间轴之类的界面。那么如何在不动原来代码的基础上添加这些横屏界面的操作呢?
1.勾选设备方向

2.为UINavigationController添加一个category
@interface UINavigationController (Autorotate)
@end
然后在.m文件中添加如下代码
- (BOOL)shouldAutorotate
{
return NO;
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIDeviceOrientationPortrait);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
3.在需要横屏的界面添加如下代码
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
注意点
在显示横屏界面时候请一定要用present方法,至于为什么不用push,你可以试一下就知道了。
效果如图

网友评论