xcode iOS Autolayout - viewDidLayoutSubviews 中未设置帧大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26629240/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:01:40  来源:igfitidea点击:

iOS Autolayout - Frame size not set in viewDidLayoutSubviews

iosiphonexcodeautolayout

提问by beeef

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.

我正在使用自动布局为 iOS 8 开发键盘以将按钮放置在视图上。
当我使用约束更改布局时,所有内容都正确显示在屏幕上,但是当我想知道视图的框架大小时,我没有得到正确的大小。

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:

例如:我按下一个键,键盘布局会根据我的约束更改并布局所有内容。然后我想知道屏幕上任何按钮的大小 - 我在“viewDidLayoutSubviews”中执行此操作并在控制台中获得该结果:

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.

该按钮具有正确的大小和正确的位置,但是在尝试获取其框架时未设置大小。

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

当我的代码在 viewDidLayoutSubviews 中不起作用时,我必须把它放在哪里?

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

我在 stackoverflow 和其他网站上发现了很多问题,但没有一个涵盖我的问题。

回答by rene

I encountered a very similar issue (upvoted question) and found beeef's answerto 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):

我遇到了一个非常相似的问题(upvoted question),并找到了Beeef 的答案指向正确的方向。鉴于 Apple 的文档说不要直接调用 layoutSubviews(),我改为执行以下操作(其中 mySubView 是 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...
}

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.

仅供参考,在我的情况下,mySubView 是一个 UIScrollView,我需要获取其中一个子视图的框架,以便我可以在视图出现之前相应地设置 contentOffset。我不确定 viewDidLayoutSubviews() 是否是执行此操作的最佳位置,但它确保 mySubView 已布局。

回答by beeef

Yes! I solved that problem myself.

是的!我自己解决了这个问题。

I'm not sure if I got it right, but that's what I think:

我不确定我是否做对了,但这就是我的想法:

  • viewDidLayoutSubviews get's called every timewhen the layout is changing
  • viewDidLayoutSubviews get's called when the direct subviews of my view did layout, but the subviews of that subviews did not layout at that moment, so you can't get the size of them.
  • 每次布局更改时都会调用 viewDidLayoutSubviews
  • viewDidLayoutSubviews 在我的视图的直接子视图进行布局时被调用,但是该子视图的子视图此时没有布局,因此您无法获取它们的大小。

My solution now was to manually call .layoutSubviews()on all my views which contains all my buttons of my keyboard. After that, I get the size of all the buttons I want.

我现在的解决方案是在我的所有视图上手动调用.layoutSubviews(),其中包含我的所有键盘按钮。之后,我得到了我想要的所有按钮的大小。

Please correct me if I'm wrong.

如果我错了,请纠正我。