ios viewDidLoad 中的错误帧大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14060002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wrong frame size in viewDidLoad
提问by Oleg
Possible Duplicate:
Why am I having to manually set my view's frame in viewDidLoad?
I have universal app for iphone and ipad, two storyboards, if I run my app on the iphone simulator in viewDidLoad
every frame of each element in nib is correct. But if I do it for ipad (simulator) in frame is nil, but the screen looks correct. Could you please advise me smth? Thanks.
我有 iphone 和 ipad 的通用应用程序,两个故事板,如果我在 iphone 模拟器上viewDidLoad
在 nib 中每个元素的每一帧中运行我的应用程序是正确的。但是,如果我为 ipad(模拟器)在框架中执行此操作为零,但屏幕看起来是正确的。你能给我建议吗?谢谢。
回答by foundry
It's a very similar question to ios setContentOffset not working on iPad... And my answer is the same as there:
这是一个与ios setContentOffset not working on iPad非常相似的问题......我的答案与那里相同:
"You should not initialise UI geometry-related things in viewDidLoad
, because the geometry of your view is not set at this point and the results will be unpredictable.
“你不应该在 中初始化 UI 几何相关的东西viewDidLoad
,因为此时你的视图的几何还没有设置,结果将是不可预测的。
... viewWillAppear:
is the correct place to do these things, at this point the geometry is set so getting and setting geometry-related properties makes sense."
...viewWillAppear:
是做这些事情的正确位置,此时几何体已设置,因此获取和设置与几何体相关的属性是有意义的。”
This also applies to gettinggeometry-related properties. Don't get distracted by it's behaving correctly in one circumstance and not in another... The main point is that setting/getting geometry properties too early will yield unpredictableresults.
这也适用于获取几何相关的属性。不要因为它在一种情况下表现正确而分心,而在另一种情况下则不然……重点是过早设置/获取几何属性会产生不可预测的结果。
Your screen lookscorrect because you are seeing the results afterviewWillAppear:
/ viewDidAppear:
.
您的屏幕看起来是正确的,因为您看到的是/之后的结果。viewWillAppear:
viewDidAppear:
update (ios6)
更新(ios6)
When using autolayout in ios6, frames are not set until subviews have been laid out. The right place to get frame data under these conditions is in the viewController method viewDidLayoutSubviews
.
在 ios6 中使用自动布局时,在布局子视图之前不会设置框架。在这些条件下获取帧数据的正确位置是在 viewController 方法中viewDidLayoutSubviews
。
This is called afterviewWillAppear:
. But take care - they are not always called together. For example, viewDidLayoutSubviews
is called after a rotation, viewWillAppear:
is not. viewWillAppear:
is called every time a view becomes the visible view*, viewDidLayoutSubviews
not necessarily (only if the views needed relaying out).
这在 之后调用viewWillAppear:
。但要小心——他们并不总是被召集在一起。例如,viewDidLayoutSubviews
在旋转后调用,viewWillAppear:
则不是。viewWillAppear:
每次视图变为可见视图时都会调用*,viewDidLayoutSubviews
不一定(仅当视图需要中继时)。
(* unless the app was backgrounded and is becoming the foreground app, then viewWillAppear:
is not called)
(* 除非该应用程序已成为后台应用程序并正在成为前台应用程序,否则viewWillAppear:
不会被调用)
回答by Oleg
I figured it out why it happened. I was using AutoLayout. When I uncheck it in the storyboard then the frames are correct.
我弄明白了为什么会这样。我正在使用自动布局。当我在故事板中取消选中它时,帧是正确的。