加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 从视图中获取容器的宽度和高度

发布时间:2020-12-14 18:08:58 所属栏目:百科 来源:网络整理
导读:如果这是重复,请原谅我.我认为这肯定是个明显的问题. 我在故事板中有一个容器,它是ModelViewController类(整个视图控制器).从ModelViewController我需要获取容器的宽度和高度.我目前正在尝试: self.view.bounds.size 产生0.563380和0.000000.我也试过了 sel
如果这是重复,请原谅我.我认为这肯定是个明显的问题.

我在故事板中有一个容器,它是ModelViewController类(整个视图控制器).从ModelViewController我需要获取容器的宽度和高度.我目前正在尝试:

self.view.bounds.size

产生0.563380和0.000000.我也试过了

self.view.frame.size

这产生了整个屏幕的尺寸.我在这里错过了什么?

解决方法

从 this post起

View frames are not actually useable in viewDidLoad; you should move all of your geometry-manipulating code into viewWillAppear. The system will clobber any changes you set in viewDidLoad between there and when it’s about tom come on screen.

评论

Actually,you should move geometry changes to -viewDidAppear pre-iOS 5 and to -viewWillLayoutSubviews for iOS 5+. -viewWillAppear: may not actually have the correct bounds/orientation if the view is being animated.

我找到了解决方案.从viewWillLayoutSubviews调用时确实给出了正确的尺寸.我从viewDidLoad调用给出了不正确的结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读