iPhone开发之在应用中从竖屏模式强制转换为横屏模式
1.强制横屏模式,百度上找到很多方法,但是真正能用到项目上的却少之又少,有的是iOS版本太低的时候出的,过时了;有的方法被Apple官方私有化了。
2.开发工具设置
3.代码实现的两种方法
(1) 此方法已经被Apple官方私有化,不能通过审核,但是用来实现简易测试非常方便
1 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
(2)直接书写会出现报错,需要巧妙的转化绕过
1 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { 2 [[UIDevice currentDevice] performSelector:@selector(setOrientation:)withObject:(id)UIInterfaceOrientationLandscapeRight]; 3 }
==>>转化后
1 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { 2 SEL selector = NSSelectorFromString(@"setOrientation:"); 3 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; 4 [invocation setSelector:selector]; 5 [invocation setTarget:[UIDevice currentDevice]]; 6 int val = UIInterfaceOrientationLandscapeRight; 7 [invocation setArgument:&val atIndex:2]; 8 [invocation invoke]; 9 }
(3)当然这样强制之后屏幕的宽高也需要重新再确定(这点在Xcode5和Xcode6版本上可能会有所不同)
pch
//Xcode5 #define HBScreenWidth [[UIScreen mainScreen] bounds].size.height #define HBScreenHeight [[UIScreen mainScreen] bounds].size.width //Xcode6 //#define HBScreenWidth [[UIScreen mainScreen] bounds].size.width //#define HBScreenHeight [[UIScreen mainScreen] bounds].size.height
.m
1 CGFloat launchWidth = HBScreenHeight; 2 CGFloat launchHeight = HBScreenWidth; 3 4 NSInteger imageCount = 4; 5 _launchScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, launchWidth, launchHeight)]; 6 _launchScrollView.bounces = NO; 7 _launchScrollView.pagingEnabled = YES; 8 _launchScrollView.showsHorizontalScrollIndicator = NO; 9 _launchScrollView.contentOffset = CGPointMake(0.0, 0.0); 10 _launchScrollView.contentSize = CGSizeMake(launchWidth * imageCount, 0);
(4) 关于NSInvocation有待进一步学习补充
http://blog.csdn.net/nogodoss/article/details/23913499
//iOS更新后的方法
单个界面强制横屏,不能自动旋转,可由以下两句代替
1 - (NSUInteger)supportedInterfaceOrientations 2 { 3 return UIInterfaceOrientationPortrait; 4 } 5 6 - (BOOL)shouldAutorotate 7 { 8 return NO; 9 }
http://wenku.baidu.com/link?url=H3l4g_vkzbBrq7FeXOk0qqP7lIiUFC-SmBpCk5eBJDXxj81srFBV3Y93KmCDnTfAzNtsQC5HBgPO8fBx6b59mN84RNKhzgkyvq7-XTciUSm