简体   繁体   English

自动布局和可调整大小的视图存在问题

[英]Problems with autolayout and resizable views

I have issue with autolayout. 我对自动版式有疑问。

I have three different views (could be n views as well). 我有三种不同的观点(也可以是n种观点)。 When I pin first one to top of super view and connect one next to the other everything seems fine. 当我将第一个固定在超级视图的顶部并将另一个紧挨着连接时,一切似乎都很好。 But if I want to change height of first one the other two does not follow (they stay on the same position). 但是,如果我想更改第一个的高度,则其他两个不会跟随(它们保持在同一位置)。 What I find weird about this is that if I connect only two views this works perfectly. 我对此感到奇怪的是,如果仅连接两个视图,则效果很好。

To have better understanding what I am trying to achieve I made simple, example solution with three views: 为了更好地理解我要实现的目标,我使用以下三种简单的示例解决方案:

http://i.stack.imgur.com/cMwiB.png http://i.stack.imgur.com/cMwiB.png

So, when I set height of green view to 0 ( can be any other number as well ) and only red view is connected, this works fine, red will move as it should. 因此,当我将绿色视图的高度设置为0(也可以是任何其他数字)并且仅连接了红色视图时,此方法工作正常,红色将按其应有的方式移动。 Blue one is not connected and it remains where it is: 蓝色的一个未连接,并保持在原来的位置:

Here is screenshot when I connect blue to red: 这是我将蓝色连接为红色时的屏幕截图:

What I tried to do: 我试图做的是:

  • connect blue one to green as well ( not working) 将蓝色也连接到绿色(不起作用)
  • connect blue only to green ( not working ) 仅将蓝色连接到绿色(不起作用)
  • "boxing": have boxes with views at the time ( ugly, hard to manage, not working in all cases) “装箱”:带有当时带有视图的包装箱(丑陋,难以管理,在所有情况下均不起作用)

What I would like to know: 我想知道的是:

  • what makes this happen? 是什么使这种情况发生? Can I change this kind of behavior? 我可以改变这种行为吗?
  • What is the best practice to have flexible length for height (Besides UITableView) 在高度上具有可变长度的最佳实践是什么(除了UITableView)

Thanks in advance. 提前致谢。

Ok, I think you're falling into the same trap I did, when I investigated autolayout some time ago. 好的,我想您在不久前调查自动版式时就陷入了同样的陷阱。 It's tricky to explain, but I'll do my best. 解释起来很棘手,但我会尽力而为。 When you work with autolayouts, you need to forget completely about setting a view's frame at all. 使用自动布局时,您完全需要完全忘记设置视图的框架。 And that, my friend, includes changing size and positioning. 我的朋友,这包括更改尺寸和位置。 When you use autolayout, you define some constraints, some "rules", that the view tries to respect when rendering the screen, so the more specific the rules are, the less random will be the behavior of the view. 使用自动布局时,您定义了一些约束,即一些“规则”,视图在渲染屏幕时会尝试遵守这些约束,因此规则越具体,视图行为的随机性就越小。 I mean, if you just define the spacing between views, you're implying that the height of the views doesn't matter, so it's possible that some views grow or shrink when the re-layout is called (that is, if you don't specify a constraint for size. You probably want to always specify the size of some views in every layout...) 我的意思是,如果仅定义视图之间的间距,则意味着视图的高度无关紧要,因此在调用重新布局时,某些视图可能会增大或缩小(也就是说,如果您不't指定尺寸限制。您可能希望始终在每个布局中指定某些视图的尺寸...)

By the way, you're allowed to violate the constraints by manually changing the frame of an element AFTER a re-layout is called, but when the re-layout method is called again, the constraints will be forced so the size and positioning of the views will change. 顺便说一句,允许您在手动调用重新布局后通过手动更改元素的框架来违反约束,但是当再次调用re-layout方法时,将强制约束,因此约束的大小和位置意见将改变。 Quick tip: an easy way to force the re-layout method is to change orientation (command+left / command+right in the simulator). 快速提示:强制重新布局方法的一种简单方法是更改​​方向(模拟器中的command + left / command + right)。

So after saying that, I have to say that the layout you provided is completely working as intended, at least to me. 因此,在说完之后,我必须说,您提供的布局完全可以按预期工作,至少对我而言。 When you change orientations the constraints you specified are ALWAYS being respected. 更改方向时,始终会遵守您指定的约束。 If you want, you can try to apply some frame-setting in the viewDidAppear method, because this one is called AFTER the autolayout (and thus, you're able to violate the constraints temporally). 如果需要,您可以尝试在viewDidAppear方法中应用一些框架设置,因为在自动布局之后将其称为(因此,您可以暂时违反这些约束)。 After doing this, your view will be broken but once you change orientation the constraints should be respected again. 完成此操作后,您的视图将被破坏,但是一旦更改方向,则应再次遵守约束条件。 Make sure it's that way. 确保是那样。

From here I can only wish you luck ;) Oh, and refer to the documentation, it was a life saver to me when I looked at this half a year ago. 从这里,我只希望您能好运;)哦,请参考文档,当我半年前查看时,它为我省了很多命。 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html It might seem a classic, but it's a pretty nice doc. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html这似乎很经典,但是它是一个很好的文档。

EDIT : one last thing. 编辑 :最后一件事。 I have the impression that constraints are not designed to be dynamic. 我的印象是约束并不是动态设计的。 You define them once, and they're there always. 您只需定义一次,它们就始终存在。 Their main purpose is to ensure that subviews are rendered as intended in every screen resolution without fail, so if I were to make an application with moving views, I would leave them outside of the autolayout, or avoid using it at all. 它们的主要目的是确保子视图在每种屏幕分辨率下都能按预期方式呈现,因此,如果我要创建带有移动视图的应用程序,我会将其置于自动布局之外,或者完全避免使用它。 Just a personal impression though :) 虽然只是个人印象:)

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

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