viewDidLoad中的帧大小错误 [英] wrong frame size in viewDidLoad

查看:146
本文介绍了viewDidLoad中的帧大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我有iphone和ipad的通用应用程序,两个故事板,如果我在iphone模拟器上运行我的应用程序 viewDidLoad nib中每个元素的每一帧都是正确的。但是,如果我在ipad(模拟器)中为帧做nil,但屏幕看起来是正确的。你能告诉我什么吗?谢谢。

这是一个非常类似的问题 ios setContentOffset无法在iPad上运行 ......我的回答与之相同:



您不应在 viewDidLoad 中初始化与UI几何相关的内容,因为此时未设置视图的几何图形,结果将无法预测。 / p>

... viewWillAppear:是做这些事情的正确位置,此时几何图形已经设置好了并设置与几何相关的属性是有意义的。



这也适用于获取几何相关的属性。不要因为它在一种情况下而不是在另一种环境中表现正常而分散注意力......主要的一点是过早地设置/获取几何属性会产生不可预测的结果。



您的屏幕看起来正确,因为您在 viewWillAppear后看到的结果: / viewDidAppear:



update(ios6)



在ios6中使用autolayout时,在布局子视图之前不会设置帧。在这些条件下获取帧数据的正确位置在viewController方法 viewDidLayoutSubviews 中。



viewWillAppear:之后,这称为。但要小心 - 他们并不总是被召集在一起。例如,旋转后调用 viewDidLayoutSubviews viewWillAppear:不是。每次视图成为可见视图时都会调用 viewWillAppear: viewDidLayoutSubviews 不一定(仅当视图需要中继时)出)。



(*除非应用程序已落后并且正在成为前台应用程序,否则 viewWillAppear:未被调用)


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.

解决方案

It's a very similar question to ios setContentOffset not working on iPad ... And my answer is the same as there:

"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.

... 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."

This also applies to getting geometry-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 unpredictable results.

Your screen looks correct because you are seeing the results after viewWillAppear: / viewDidAppear:.

update (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.

This is called after viewWillAppear:. 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).

(* unless the app was backgrounded and is becoming the foreground app, then viewWillAppear: is not called)

这篇关于viewDidLoad中的帧大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆