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

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

问题描述

我不是在谈论 frame 属性,因为从中你只能在 xib 中获取视图的大小.我指的是视图何时因约束而调整大小(可能在旋转之后,或响应事件).有没有办法获得它当前的宽度和高度?

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!

推荐答案

答案是[view layoutIfNeeded].

原因如下:

您仍然可以通过检查 view.bounds.size.widthview.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 in order to interpret those constraints. 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.

你如何要求自动布局来更新布局?调用 [view 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.

但是,如果您希望它立即更新布局,以便您可以稍后在当前函数中立即访问新的边界值,或者在轮到运行循环之前的另一点,则您需要调用 [view setNeedsLayout][view 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,以便您可以直接引用它.如果您在代码中创建了约束,那么您应该在创建它时保留内部弱属性中的引用.如果其他人创建了约束,那么您需要通过检查视图上的 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天全站免登陆