我如何使用自动布局约束时,得到一个视图当前的宽度和高度? [英] How can I get a view's current width and height when using autolayout constraints?

查看:327
本文介绍了我如何使用自动布局约束时,得到一个视图当前的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说的不是框架性质,因为你只能在厦门国际银行获取视图的大小。我说当视图大小,因为它的约束(也许是旋转后,或响应事件)有关。有没有办法让当前的宽度和高度?

I'm not talking about the frame property, because from that you can only get the view's size in the xib. I'm talking about when the view is resized because of its constraints (maybe after a rotation, or in response to an event). Is there a way to get its current width and height?

我试图通过其约束在寻找的宽度和高度的限制迭代,但是这不是很干净,当有内在的约束(因为我不能区分这两种)失败。另外,如果他们真的有宽度和高度的限制,如果他们依靠其他方面的限制来调整他们不只有工作。

I tried iterating through its constraints looking for width and height constraints, but that's not very clean and fails when there are intrinsic constraints (since I can't differentiate between the two). Also, that only works if they actually have width and height constraints, which they don't if they rely on other constraints to resize.

为什么这对我来说非常困难。 ARG!

Why is this so difficult for me. ARG!

推荐答案

答案是: [查看layoutIfNeeded]

下面的原因:

您还通过检查获取视图当前的宽度和高度 view.bounds.size.width view.bounds.size.height (或框架,除非你摆弄这相当于在 view.transform )。

You still get the view's current width and height by inspecting view.bounds.size.width and view.bounds.size.height (or the frame, which is equivalent unless you're playing with the view.transform).

如果你想要的是宽度和高度由现有的约束暗示,答案是没有手动检查约束,因为这将要求您重新实现自动排版系统的整个约束求解逻辑。相反,你应该做的是刚刚的要求自动布局更新布局的,因此,它解决了约束,并更新了正确的解决方案view.bounds的值,然后检查view.bounds

If what you want is the width and height implied by your existing constraints, the answer is not to inspect the constraints manually, since that would require you to re-implement the entire constraint-solving logic of the auto layout system. Instead, what you should do is just ask auto layout to update that layout, so that it solves the constraints and updates the value of view.bounds with the correct solution, and then you inspect the view.bounds.

您怎么要求自动布局更新布局?呼叫 [查看setNeedsLayout] 如果你想自动布局更新运行循环的下个回合的布局。

How do you ask auto layout to update the layout? Call [view setNeedsLayout] if you want auto layout to update the layout on the next turn of the run loop.

然而,如果您要立即更新布局,让您可以立即运行循环交替之前访问新的边界值后在当前的功能,或在另一点,然后您需要调用 [查看setNeedsLayout] [查看layoutIfNeeded]

However, if you want it to update the layout immediately, so you can immediately access the new bounds value later within your current function, or at another point before the turn of the run loop, then you need to call [view setNeedsLayout] and [view layoutIfNeeded].

您问的第二个问题:我怎么可以改变的高度/宽度约束,如果我没有直接引用它?。

You asked a second question: "how can I change a height/width constraint if I don't have a reference to it directly?".

如果您创建IB约束,最好的解决办法是在你的视图控制器或视图中创建一个IBOutlet所以你必须直接引用它。如果你创建了code中的约束,那么你应该稳守在当时的内部弱属性的引用,当你创造了它。如果其他人创建的约束,那么你需要通过检查对视图进行检查view.constraints属性来找到它,可能是整个视图层次结构,并执行逻辑,找到关键NSLayoutConstraint。这可能是错误的路要走,因为它也有效地要求你确定哪些特定的约束条件确定的界限大小,当出现不能保证是一个简单的问题的答案。最后的界限值可以解决的多重限制一个非常复杂的系统,具有多项优先权,等等,所以没有一个约束是最终值的事业。

If you create the constraint in IB, the best solution is to create an IBOutlet in your view controller or your view so you do have a direct reference to it. If you created the constraint in code, then you should hold onto a reference in an internal weak property at the time when you created it. If someone else created the constraint, then you need to find it by examining the examining the view.constraints property on the view, and possibly the entire view hierarchy, and implementing logic which finds the crucial NSLayoutConstraint. This is probably the wrong way to go, since it also effectively requires you to determine which particular constraint determined the bounds size, when there's not guaranteed to be a simple answer to that question. The final bounds value could be the solution to a very complicated system of multiple constraints, with multiple priorities, etc., so that no single constraint is the "cause" of the final value.

这篇关于我如何使用自动布局约束时,得到一个视图当前的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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