您的位置:首页 > 产品设计 > UI/UE

NavigationController单个界面横屏,其他全竖屏

2014-01-15 17:29 639 查看
由于自己没有mac设备,x200装黑苹果失败,本文的代码全靠记忆,如有错误之处敬请指正。

最近在做一个带有报表的iOS App开发,要求从记录界面转到图表界面时从竖屏转为横屏。

首先在info.plist中有关于app支持的屏幕方向设置,默认支持竖屏和横屏(iphone默认不支持倒立)。如果想让自己的app只支持横屏或者竖屏建议在该文件中修改,比在代码中修改要方便得多。

在storyboard或者ib中,需要将横屏的XXViewController的方向属性(orientation)改为Landscape。

如果使用modal等方式切换界面,经过以上步骤,就可以看到载入的界面为横屏。

如果使用push,需要在横屏ViewController中添加以下代码:

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
如果需要横屏的ViewController不是NavigationController push过来的话,就可以看到载入界面后自动旋转成横屏了。

网上的说法是这样不保险,更安全(过苹果的审核)的方法是更改view.transform属性。这样需要的操作太多,由于项目离发布还有一段时间,我尝试了一下,发现旋转后导航栏的高度没有自动调整,便暂时搁置了。

如果在使用NavigationController时,需要限定某些界面为竖屏,而另外一些界面为横屏或者可以旋转,需要新建一个UINavigationController的子类,根据需要重写它的一些方法,如下所示。然后在StoryBoard中将NavigationController的root的class设为新建的UINavigationController的子类。

// 支持的设备方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[XxxXxxXxx class]]) {
return UIDeviceOrietationMaskXxx;
} else {
return UIDeviceOrietationMaskXxx;
}
}

// 是否支持自动旋转
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[XxxXxxXxx class]]) {
return YES;
}
return NO;
}

// 支持的界面方向
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息