横竖屏切换,视图乱了怎么办?

首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向:

UIInterfaceOrientationLandscapeLeft

向左,即HOME键在右

UIInterfaceOrientationLandscapeRight

向右,即HOME键在左

UIInterfaceOrientationPortrait

正立,即HOME键在下

UIInterfaceOrientationPortraitUpsideDown

倒立,即HOME键在上

对于旋屏的处理,大致分为如下几种情况和思路:

也许,你不需要旋屏支持,而希望锁定屏幕

  1. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  2. {
  3. return NO;
  4. }

也许,你需要支持旋屏,或者支持部分方向的旋屏

  1. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  2. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  3. }

也许,你的view有张背景图,旋屏时系统帮助你拉伸了图片,但是却没有管你的其它部件,比如button,你希望直接改变button的大小和位置

  1. -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  2. {
  3. if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
  4. NSLog(@"现在是竖屏");
  5. [btn setFrame:CGRectMake(213, 442, 340, 46)];
  6. }
  7. if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
  8. NSLog(@"现在是横屏");
  9. [btn setFrame:CGRectMake(280, 322, 460, 35)];
  10. }
  11. }

也许,你并不希望用绝对坐标去约束控件,而是希望让它通过旋转自己适应屏幕的旋转

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. UIDevice *device = [UIDevice currentDevice];
  6. [device beginGeneratingDeviceOrientationNotifications];
  7. //利用 NSNotificationCenter 获得旋转信号 UIDeviceOrientationDidChangeNotification
  8. NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];
  9. [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];
  10. }
  11. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  12. {
  13. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  14. }
  15. -(void)rotation_btn:(float)n
  16. {
  17. UIButton *robtn = self.btn;
  18. robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  19. }
  20. -(void)orientationChanged
  21. {
  22. UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];
  23. switch (orientaiton) {
  24. caseUIDeviceOrientationPortrait:
  25. [self  rotation_btn:0.0];
  26. break;
  27. caseUIDeviceOrientationPortraitUpsideDown:
  28. [self  rotation_btn:90.0*2];
  29. break;
  30. caseUIDeviceOrientationLandscapeLeft:
  31. [self  rotation_btn:90.0*3];
  32. break;
  33. caseUIDeviceOrientationLandscapeRight:
  34. [self  rotation_btn:90.0];
  35. break;
  36. default:
  37. break;
  38. }
  39. }

也许,你需要autoresizesSubviews = YES

也许,你希望横竖屏有不同的布局效果,需要准备2份Subview,在不同状态去替换

当然不要忘记,需要调节设定图示中的1、2处,

来帮助我们完成自己想要的适应效果。Example 动画呈现的很清晰,^_^ 我就不再啰嗦了。

最新文章

  1. Mina、Netty、Twisted一起学(七):发布/订阅(Publish/Subscribe)
  2. MYSQL管理之主从同步管理
  3. JS数组定义【收藏】
  4. 漂亮的title提示信息
  5. 2. 星际争霸之php面向对象(二)
  6. 创建ubuntu软件源
  7. Intellij 导入play framework 项目
  8. Promise和异步编程
  9. Online Judge(OJ)搭建——2、数据库,SQL语句
  10. SQL Server 查询性能优化——创建索引原则(二)
  11. SHELL脚本--简介
  12. mysql 中实现多条数据同时更新
  13. MyBatis基础入门《六》Like模糊查询
  14. C++ 求最长递增子序列(动态规划)
  15. 20145234黄斐《Java程序设计》第七周
  16. HTML一(简介)
  17. 连接数据库的DBUtils工具类
  18. 做开发,你少不了的淘宝镜像之--maven镜像
  19. Python对文本文件逐行扫描,将含有关键字的行存放到另一文件
  20. js 复制到剪贴板 兼容还得自己想办法

热门文章

  1. DirectX实现球面纹理映射
  2. UnityEngine中Animator相关类的说明
  3. Android真机调测Profiler
  4. shell学习(3)- grep
  5. 解决“每次启动Access2010时都要求配置VS2008”的办法
  6. 101 to 010 Atcoder CODE FESTIVAL 2017 qual B D
  7. java线性表之顺序表实现
  8. 再看SpringMVC通过一个DispatcherServlet处理Servlet
  9. npm 修改源地址
  10. 基于.NET网页开发的工作,需要掌握的知识点