IOS 屏幕横转后怎么判断屏幕的width和height

卧看牵牛织女星 2015-05-28 04:57:30
本来判断长宽是用

float height = self.view.bounds.size.height;
float width = self.view.bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);

打印结果是
2015-05-28 16:47:53.656 TabbarDemo[9534:187551] width:320.000000
2015-05-28 16:47:53.657 TabbarDemo[9534:187551] height:568.000000


然后我注册通知监视屏幕方向。当我屏幕横转后,我再次判断屏幕的宽高

float height = self.view.bounds.size.height;
float width = self.view.bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);

打印结果还是
320
568



这是为什么啊!我屏幕横转了,这时候的width 和height不应该是568,320吗?
...全文
967 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ReyZhang 2015-05-29
  • 打赏
  • 举报
回复
引用 4 楼 u011103194 的回复:
[quote=引用 2 楼 zhanglei5415 的回复:] 不要使用self.view.bounds 来获取屏幕的尺寸。正确的做法:
CGRect bounds = [UIScreen mainScreen].bounds;
float height =bounds.size.height;
float width = bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);
不是这个问题,我试过了[/quote] 在哪儿做的监听? 如果是在viewController 中监听屏幕的旋转,你可以在viewWillLayoutSubviews 这个方法中来处理屏幕旋转后的布局。如果是在UIView中,监听屏幕旋转时会触发它的LayoutSubview 方法,在这个方法中来重新布局。 在这两个方法内使用上面我提供的方法,查看屏幕尺寸的输出。
  • 打赏
  • 举报
回复
引用 2 楼 zhanglei5415 的回复:
不要使用self.view.bounds 来获取屏幕的尺寸。正确的做法:
CGRect bounds = [UIScreen mainScreen].bounds;
float height =bounds.size.height;
float width = bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);
不是这个问题,我试过了
  • 打赏
  • 举报
回复
引用 1 楼 shirugege 的回复:
设备是不会自动去修改的 这个是不变的 你可以当获取到设备横屏之后重新设置宽高 height=width width=height ;
不对,是可以识别的,因为我在a页面是用storyboard,b页面是纯代码,如果我在a页面进行过页面横转,然后跳到b页面就一切正常
ReyZhang 2015-05-29
  • 打赏
  • 举报
回复
不要使用self.view.bounds 来获取屏幕的尺寸。正确的做法:
CGRect bounds = [UIScreen mainScreen].bounds;
float height =bounds.size.height;
float width = bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);
  • 打赏
  • 举报
回复
引用 5 楼 zhanglei5415 的回复:
[quote=引用 4 楼 u011103194 的回复:] [quote=引用 2 楼 zhanglei5415 的回复:] 不要使用self.view.bounds 来获取屏幕的尺寸。正确的做法:
CGRect bounds = [UIScreen mainScreen].bounds;
float height =bounds.size.height;
float width = bounds.size.width;
NSLog(@"width:%f",width);
NSLog(@"height:%f",height);
不是这个问题,我试过了[/quote] 在哪儿做的监听? 如果是在viewController 中监听屏幕的旋转,你可以在viewWillLayoutSubviews 这个方法中来处理屏幕旋转后的布局。如果是在UIView中,监听屏幕旋转时会触发它的LayoutSubview 方法,在这个方法中来重新布局。 在这两个方法内使用上面我提供的方法,查看屏幕尺寸的输出。[/quote] 找到问题了,是因为UIApplicationDidChangeStatusBarFrameNotification UIDeviceOrientationDidChangeNotification 这两个通知不同引起的。UIDeviceOrientationDidChangeNotification会判断所有方向,比如屏幕向上,这时候width和height是不会变的,改成UIApplicationDidChangeStatusBarFrameNotification就好了
shirugege 2015-05-28
  • 打赏
  • 举报
回复
设备是不会自动去修改的 这个是不变的 你可以当获取到设备横屏之后重新设置宽高 height=width width=height ;

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧