iOS的自动布局 - 帧大小viewDidLayoutSubviews未设定 [英] iOS Autolayout - Frame size not set in viewDidLayoutSubviews

查看:203
本文介绍了iOS的自动布局 - 帧大小viewDidLayoutSubviews未设定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在键盘上适用于iOS 8使用自动布局放置在视图上的按钮。结果
当我使用约束更改布局,一切都正确出现在屏幕上,但是当我想知道视图的帧大小,我没有得到正确的大小。

I'm working on a keyboard for iOS 8 using Autolayout to place the buttons on the view.
When I'm changing the layout using constraints, everything is appearing correctly on the screen, but when I want to know the view's frame size, I don't get the right size.

例如:我根据我的约束preSS一键,键盘布​​局的变化和布局的一切。然后,我想知道屏幕上的任何按钮的大小 - 我这样做,在viewDidLayoutSubviews,并得到这一结果在控制台:

For example: I press a key, the keyboard layout changes and layouts everything according to my constraints. Then I want to know the size of any button on the screen - I do that in "viewDidLayoutSubviews" and get that result in the console:

2014-10-29 12:27:09.088 Keyboard[2193:60674] view did layout subviews
2014-10-29 12:27:09.088 Keyboard[2193:60674] {{inf, inf}, {0, 0}}

按钮具有正确的大小和正确的位置上,而是试图获取其帧时没有设置大小。

The button has the correct size and correct position, but when trying to get its frame the size is not set.

在哪里我已经把我的code时,它不是在viewDidLayoutSubviews工作?

Where do I have to put my code when it is not working in viewDidLayoutSubviews?

我发现了很多的websited在计算器和其他问题,但他们没有覆盖我的问题。

I found a lot of questions on stackoverflow and other websited, but none of them covered my question.

推荐答案

我遇到了一个非常类似的问题(upvoted的问题),并发现 beeef的回答在正确的方向指向。鉴于苹果的文件说,不要调用layoutSubviews()直接,而不是我做了如下(其中mySubView是self.view的直接子视图):

I encountered a very similar issue (upvoted question) and found beeef's answer to point in the right direction. Given that Apple's documentation says not to call layoutSubviews() directly, I instead did as follows (where mySubView is a direct subview of self.view):

override func viewDidLayoutSubviews() {
    // self.view's direct subviews are laid out.
    // force my subview to layout its subviews:
    self.mySubView.setNeedsLayout()
    self.mySubView.layoutIfNeeded()

    // here we can get the frame of subviews of mySubView
    // and do useful things with that...
}

仅供参考,在我的情况mySubView是一个UIScrollView,我需要得到它的子视图中的一个,这样我可以设置相应的contentOffset出现视图之前的框架。我不知道viewDidLayoutSubviews()是否做到这一点的最佳场所,但它可以确保mySubView已经奠定了。

Just FYI, in my case mySubView is a UIScrollView, and I need to get the frame of one of its subviews, such that I can set the contentOffset accordingly before the view appears. I'm not sure whether viewDidLayoutSubviews() is the best place to do this, but it ensures that mySubView has been laid out.

这篇关于iOS的自动布局 - 帧大小viewDidLayoutSubviews未设定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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