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

转载于:https://www.cnblogs.com/HHB17/p/4176699.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值