AutoLayout:UIView中的UIView具有不正确的帧 [英] AutoLayout: UIView within UIView has incorrect frame

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

问题描述

我在故事板中使用AutoLayout,一切似乎都很好。然而,当我把一个UIView在另一个并应用所有的约束我想要的容器和它的孩子,我注意到框架在 viewDidLayoutSubviews:不正确。而不是我期望计算给定的约束框架,它是一些令人讨厌的大框架(虽然在正确的起源)。例如,不是 {{26,10},{444,10}}的帧,我得到类似 {{0,0},{320,568}} 。奇怪的是,这个只有 发生在子UI​​View当我把它放在另一个UIView有一些约束应用到它的superview(这是视图控制器的视图)。由于我的印象是,我可以期望我的意见根据我的约束在方法 viewDidLayoutSubviews:正确布局,我很困惑为什么这种情况发生。



我有不正确的假设吗?我会感激的,如果有人可能会帮助我指出正确的方向。非常感谢!



注意:如果我在 viewDidAppear: ,但这对我来说是一个不太满意的解决方法。

解决方案

自动布局更改边界不一定在 -viewDidLayoutSubviews 。如高级自动布局工具箱所示:


基于约束的布局是一个迭代过程。布局传递可以
基于先前的布局解决方案来改变约束,
,它再次触发在另一布局
传递之后更新约束。




-viewDidLayoutSubviews 注意事项,原文中强调的是:


但是,此方法称为不表示视图的子视图的各个布局已调整。


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



因为你说你的设置是框架依赖,你有几个选项。


  1. -viewWillAppear 中进行设置。比 -veiwDidAppear 更好,因为你真的需要所有的布局后,所有的布局,如果完成,但它出现在屏幕上之前,这是一个合法的理由,为什么方法是在那里。如果相关,您可以尝试按 -isMovingToParentViewController
    ViewControllerPGforiPhoneOS / RespondingtoDisplay-Notifications / RespondingtoDisplay-Notifications.html#// apple_ref / doc / uid / TP40007457-CH12>确定为什么视图的外观已更改

  2. 在您需要正确值的视图上调用 -layoutIfNeeded 。因为这样做的布局工作是昂贵的,它可能导致重复工作,甚至是一个无限循环,如果在自己的布局过程中触发。但是如果需要使用Auto Layout来同步某些值以便可以使用,则非常有用。

如果这不起作用,知道,它可能与约束本身或其他的性质有关。自动布局迭代可能很难同步。祝你好运。


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

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!

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.

解决方案

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.

The documentation for -viewDidLayoutSubviews notes with emphasis in the original:

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

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. Do your setup in -viewWillAppear. Better than -veiwDidAppear, 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.
  2. 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.

这篇关于AutoLayout:UIView中的UIView具有不正确的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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