最近在自己的项目里面  有需要做一个需求 : app中某一个页面支持横竖屏, 而其他页面只能竖屏。
  • 1
  • 2

实现方法如下: 
1 首先需要Xcode中选中支持的屏幕方向 

2 Appdelegate中 
.h

@property (nonatomic,assign)NSInteger allowRotate; 
  • 1

.m中

//此方法会在设备横竖屏变化的时候调用
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{ // NSLog(@"方向 ============= %ld", _allowRotate);
if (_allowRotate == 1) {
return UIInterfaceOrientationMaskAll;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
} // 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (_allowRotate == 1) {
return YES;
}
return NO;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3 在需要支持横竖屏的controller中:

viewWillApplear 中

 //在视图出现的时候,将allowRotate改为1,
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 1;
  • 1
  • 2
  • 3

viewWillDisappear中

 //在视图出现的时候,将allowRotate改为0,
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 0;
  • 1
  • 2
  • 3

写好以上代码之后, 会发现一些问题: 当横屏页面直接点击“返回”按钮退出的时候, 页面依然是横屏, 而我们需要的是仅一个页面可以横屏,测试需要在viewWillDisappear中加入如下代码:

    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 = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

此时就可以使app仅有设置页面支持横竖屏了!

此时如果app要求用户在横屏 竖屏的模式下改变UI(横屏与竖屏对应不同的UI), 可以在以下方法中执行

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// do something before rotation
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
屏幕从竖屏变为横屏时执行 }else{
屏幕从横屏变为竖屏时执行
}
} - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// do something after rotation }

最新文章

  1. linux驱动程序设计的硬件基础,王明学learn
  2. Gulp Babel AMD转换例子
  3. 【转载】about slack
  4. margin系列之内秀篇
  5. 用 Eclipse 开发 Android 应用程序
  6. 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
  7. CoreAnimation1-图层树、寄宿图以及图层几何学
  8. android Handler及消息处理机制的简单介绍
  9. JavaSctipr 兼容、技巧、牛角尖
  10. Gulp思维——Gulp高级技巧
  11. INNO SETUP数据库的连接与创建
  12. Linux和Shell教程
  13. shiro框架的UsernamePasswordToken与对应Realm中的AuthenticationToken的一点比较
  14. python按行遍历一个大文件,最优的语法应该是什么?
  15. Python3+qrcode+zxing生成和识别二维码教程
  16. Nginx自学笔记
  17. BZOJ2322 [BeiJing2011]梦想封印 【set + 线性基】
  18. Teamwork(The first day of the team)
  19. C++中的字节对齐分析
  20. python 实现查找某个字符在字符串中出现次数,并以字典形式输出

热门文章

  1. 牛客国庆集训派对Day1.B.Attack on Titan(思路 最短路Dijkstra)
  2. My Web Developer Roadmap
  3. 高质量PHP代码的50个实用技巧:非常值得收藏
  4. 基于socket构造c/s 架构软件
  5. BZOJ3945 : 无聊的邮递员
  6. NOIP考试各种技巧!!
  7. meta总结
  8. selenium+PhantomJS小案例—爬豆瓣网所有电影代码python
  9. Mac使用Clion配置OpenGL
  10. C_求质数