自动布局:UIView 中的 UIView 有不正确的框架

AutoLayout: UIView within UIView has incorrect frame(自动布局:UIView 中的 UIView 有不正确的框架)
本文介绍了自动布局:UIView 中的 UIView 有不正确的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Storyboard 中使用 AutoLayout,一切似乎都很顺利.但是,当我将一个 UIView 放入另一个 UIView 并对容器及其子级应用我想要的所有约束时,我注意到 viewDidLayoutSubviews: 中的框架不正确.考虑到我的限制,它不是我期望计算的框架,而是一些令人作呕的大框架(尽管在正确的原点).例如,我得到的不是 {{26, 10}, {444, 10}} 的框架,而是 {{0, 0}, {320, 568}}时间>.

I am working with AutoLayout in Storyboard, and everything seemed to be going fine. However, when I put one UIView within another and apply all the constraints I want to both the container and its children, I notice that the frame is incorrect in viewDidLayoutSubviews:. Rather than being the frame I would expect to be calculated given my constraints, it is some disgustingly large frame (albeit at the correct origin). For example, rather than a frame of {{26, 10}, {444, 10}} I get something like {{0, 0}, {320, 568}}.

奇怪的是,当我将子 UIView 置于另一个 UIView 中时,这种情况发生,该 UIView 对其父视图(即视图控制器的视图)应用了一些约束.由于我的印象是,我可以期望我的视图可以根据我在方法 viewDidLayoutSubviews: 中的约束正确布局,我对为什么会发生这种情况感到困惑.

Strangely enough, this only happens to the child UIView when I situate it within another UIView that has some constraints applied to it in relation to its superview (which is the view controller's view). As I am under the impression that I can expect my views to be properly laid out according to my constraints in the method viewDidLayoutSubviews:, I am confused as to why this is happening.

我是否做出了不正确的假设?如果有人可以帮助我指出正确的方向,我将不胜感激.谢谢!

Am I making any incorrect assumptions? I would appreciate it if someone might help point me in the right direction. Thanks!

注意:如果我在 viewDidAppear: 中进行依赖于正确帧的设置,这个问题就完全解决了,但这对我来说有点不满意.

Note: This problem is completely fixed if I do my correct-frame-dependent setup in viewDidAppear:, but this is a somewhat unsatisfying workaround for me.

推荐答案

对边界的自动布局更改不一定在 -viewDidLayoutSubviews 中完成.正如 "Advance Auto Layout Toolbox":

Auto Layout changes to bounds are not necessarily finished in -viewDidLayoutSubviews. As put in "Advance Auto Layout Toolbox":

基于约束的布局是一个迭代过程.布局通行证可以根据之前的布局解决方案对约束进行更改,这再次触发在另一个布局之后更新约束通过.

Constraint-based layout is an iterative process. The layout pass can make changes to the constraints based on the previous layout solution, which again triggers updating the constraints following another layout pass.

-viewDidLayoutSubviews 的文档 原文中强调的注释:

然而,这个被调用的方法并不表明视图的子视图的个别布局已经被调整.

However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted.

所以子视图中的布局和基于约束的迭代可以继续调整它们的框架,但是如果 ViewController 的视图边界没有改变,它的 -viewWillLayoutSubviews 将不会被调用.

So layout and constraint-based iterations in the subviews could continue to adjust their frames, but if a ViewController's view's bounds don't change, its -viewWillLayoutSubviews won't get called.

既然你说你的设置是帧相关的";你有几个选择.

Since you say your setup is "frame-dependent" you have a couple options.

<罢工>1.在 -viewWillAppear 中进行设置.比 -viewDidAppear 更好,因为你真的需要在所有布局完成后完成所有的事情,但在它出现在屏幕上之前,这是该方法存在的正当理由.如果相关,您可以尝试调用 -isMovingToParentViewController如 确定视图的外观为何改变.

1. Do your setup in -viewWillAppear. Better than -viewDidAppear, and since you really need everything to be done after all the layout if completed but before it appears on screen, this is a legitimate reason why the method is there. If relevant, you could try calling -isMovingToParentViewController as described in Determining Why A View's Appearance Changed.

-viewWillAppear 在视图添加到视图层次结构之前调用,因此布局可能不会完成.抱歉,感谢 Swift UI,我们正在从过程式布局转向声明式布局,这就是原因.

-viewWillAppear is called before the view is added to the view hierarchy and therefor layout may not be completed. Sorry everyone, and thankfully with Swift UI we are moving away from procedural layout to declarative layout, and this is why.

  1. 在视图上调用 -layoutIfNeeded 以获得正确的值.因为这样做布局工作很昂贵,并且如果在其自己的布局过程中触发,它可能导致重复工作甚至无限循环.但是,如果需要将某些值与 Auto Layout 同步以便可以使用它们,它会很有用.
  1. Call -layoutIfNeeded on the views you need to have the correct values where you need them to. Because this does the layout work it's expensive, and it could lead to repetitive work being done or even an infinite loop if triggered during its own layout process. But it's useful if it's needed to sync some values with Auto Layout so they can be used.

如果这不起作用,请告诉我们,这可能与约束本身的性质或其他原因有关.自动布局迭代可能很难与之同步.祝你好运.

If this doesn't work let us know, it could have to do with the nature of the constraints themselves or something else. The Auto Layout iterations can be tricky to sync with. Good luck.

这篇关于自动布局:UIView 中的 UIView 有不正确的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)