在手机开发过程中,你可能会需要你的手机横过来看,有可能是全部界面都要横过来,有可能是当用户把手机横过来的时候,你的界面也想让他横过来,也有可能是只有部分界面需要横着显示的,根据不同的情况,有不同的解决办法。
首先,我们要理清,方向分两种,一种是设备的方向,一种是视图方向。设备方向有两种方式可以改变,一个是通过重力加速计,即旋转屏幕的方式去改变,一个是通过代码,调用UIDevice的方式去改变!直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险。
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
}
我们都是通过改变视图的方向来让屏幕旋转的。
情景一:程序中所有界面都是横屏显示的
解决办法:修改工程配置文件plist,里面的UISupportedInterfaceOrientations属性表示程序支持的方向,我们把它改成只支持Left和Right
<key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array>UIInterfaceOrientationPortrait表示Home键按钮的方向,也就是竖屏方向,不要他
这样,程序启动的时候就是横屏显示了,当然你需要使用横屏的插件来设计界面了,可以使用
Interface Builder工具
解决办法二:调整window的方向,这样就不用每个view都改变了
设置状态条的方向和使用transform旋转一下window.不翻状态条的方向,会出现键盘也不会跟着翻.如果只是翻了某个view,那么会出现其它view不会翻.对于我的具体情况,翻window最简单.
UIApplication *application=[UIApplication sharedApplication]; [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);注:需要设置程序不会自动响应自动旋转
//因为想要手动旋转,所以先关闭自动旋转 - (BOOL)shouldAutorotate{ return NO; }情景二:程序中只有某个view需要横屏显示,手动设置,不支持重力加速器感应
解决办法:在指定的ViewController里面进行设置,取消自动旋转,调用CGAffineTransformMakeRotation让视图旋转,并重新定义大小
这时plist里面就不能像之前那么设置了,要把UIInterfaceOrientationPortrait也加进去,因为还有别的页面是竖屏的
首先关闭自动旋转,在ViewController.m里面加上这句
//因为想要手动旋转,所以先关闭自动旋转 - (BOOL)shouldAutorotate{ return NO; }然后就是实现旋转的代码了,我们使用的是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//实例化chart
cv =[[ChartViewBase alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
[self.view addSubview:cv];
//旋转屏幕,但是只旋转当前的View
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
CGRect frame = [UIScreen mainScreen].applicationFrame;
self.view.bounds = CGRectMake(0, 0, frame.size.height, 320);
}
注意:
1. 只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的话,下面设置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情况下才管用!
情景三:程序需要支持重力感应,当用户把手机横过来,程序也要横过来
解决方案:重新以下两个方法
- (BOOL)shouldAutorotate; - (NSUInteger)supportedInterfaceOrientations;
IOS6里面,控制某个viewController旋转并不是像IOS5或者IOS4一样在这个viewController里面重写上面那2个方法,而是需要在这个viewController的rootViewController(根视图控制器)里面重写,怎么解释呢?就是最前面的那个viewController,直接跟self.window接触的那个controller。
UIViewController *viewCtrl = [[UIViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
if ([window respondsToSelector:@selector(setRootViewController:)]) {
self.window.rootViewController = navCtrl;
} else {
[self.window addSubview:navCtrl.view];
}
如果需要设置viewCtrl的旋转,那么不能在UIViewController里面重写shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面设置,又因为UINavigationController是系统控件,所以这里需要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比如:
-(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAllButUpsideDown; } - (BOOL)shouldAutorotate{ return YES; }