您的位置:首页 > 移动开发 > IOS开发

iOS 强制转成横屏的方式

2015-01-06 16:41 183 查看
手里的项目需要在竖屏的情况下有个别页面进行横屏强制切换,困扰了一天终于找到解决的办法。办法由如下两个:

(1)手动改变view.transform属性

简明的说就是旋转你的view,将view旋转后强迫用户进行横屏操作

self.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
先设置一个横屏状态的view 接下来在添加你要的控件
接下来就是将你的view进行旋转了,代码如下:
self.view.center = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
[self.view setTransform:transform];


好了,现在可以看到你的view大致已经达到了你想要的结果,如果你是隐藏状态栏,如果你的uiview中存在UItextfield你会发现你的键盘还是从竖屏状态出现,键盘的方向跟状态栏有关,修改你的状态栏方向即可
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
在实际操作过程中 我发现一个问题:在横屏界面退至后台再进入前台会发现界面又旋转了90...看了半天不知道问题在哪里,估计是状态栏的原因,我用了第二种方法
(2)通过setOrientation:强制旋转到一个特定的方向
网上看了下,Apple在3.0后不支持此方法了,已经成为私有方法了,要跳过App Store的审核有个巧妙的方法:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
但是要在子试图中重写下面几个方法:
-(BOOL)shouldAutorotate {

NSLog(@"UINavigationController 100");
// 不想其子页面支持旋转, 可直接返回 NO
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return toInterfaceOrientation==UIInterfaceOrientationLandscapeRight;
}


但是如果第一种方法就能满足你最好就用第一种,因为第二种毕竟存在一定的风险
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: