简体   繁体   English

自动布局问题

[英]Problems with autolayout

I'm having problems with auto layout. 我在自动布局时遇到问题。

What I need to do: 我需要做什么:
Adjust a view (V1) using auto layout and than set a different view(V2) (custom view with charts) inside of it. 使用自动布局调整视图(V1),然后在其中设置其他视图(V2)(带有图表的自定义视图)。

The Problem: 问题:
When I set the width and height of V1 in storyboard its frame is: 0,0, 600, 300 (size classes is on), at runtime the width of superview is 320, for exemple, but when I try to see the frame of V1 during runtime it still with width 600. 当我在情节提要板中设置V1的宽度和高度时,其框架为:0,0,600,300(打开大小类),例如,在运行时,超级视图的宽度为320,但是当我尝试查看的框架时, V1在运行时仍具有600的宽度。

My Question: 我的问题:
Exist a way to find out which is the "real" frame of V1? 存在一种方法来找出哪个是V1的“真实”框架? The width value its assume at runtime? 在运行时假定的宽度值?

First of all, the reason for the problem with finding out the frame is that you are asking for the frame at the wrong time, ie too soon. 首先,找出框架存在问题的原因是您在错误的时间(即过早)请求框架。 You have to wait until after layout has occurred. 你必须等待,直到发生布局。

But the real answer is that you are thinking about this completely wrong. 但是真正的答案是您正在考虑完全错误。 Create V2 and make it a subview of V1, and position and size it using constraints . 创建V2并使其成为V1的子视图,并使用constraints对其进行定位和调整大小。 Use auto layout here too! 在这里也使用自动布局! That way, it will be right, relative to V1, no matter what happens to V1. 这样,无论V1发生什么 ,相对于V1都是正确的。 You never have to know what the frame of V1 is! 永远不必知道V1的框架是什么! That is the whole point of autolayout !! 这就是自动布局的重点 Don't subvert autolayout or try to work around it; 不要破坏自动布局或尝试解决它; use it! 使用它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM