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

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

问题描述

<块引用>

可能的重复:
为什么我必须手动设置我的viewDidLoad 中的视图框架?

我有适用于 iphone 和 ipad 的通用应用程序,两个故事板,如果我在 viewDidLoad 中的 iphone 模拟器上运行我的应用程序,nib 中每个元素的每一帧都是正确的.但是,如果我为 ipad(模拟器)在框架中执行此操作为零,但屏幕看起来是正确的.你能给我建议吗?谢谢.

解决方案

这是一个与 ios setContentOffset 在 iPad 上不起作用 ...我的回答和那里一样:

你不应该在 viewDidLoad 中初始化 UI 几何相关的东西,因为此时你的视图的几何还没有设置,结果将是不可预测的.

... viewWillAppear: 是执行这些操作的正确位置,此时几何体已设置,因此获取和设置与几何体相关的属性是有意义的."

这也适用于获取几何相关的属性.不要因为它在一种情况下表现正确而分心,而在另一种情况下则不然……重点是过早设置/获取几何属性会产生不可预测的结果.

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

更新 (ios6)

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

这被称为 after 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天全站免登陆