简体   繁体   English

AutoLayout - 如何绑定两个视图并相应地更改大小

[英]AutoLayout - how to bind two view and change size accordingly

i want something who seems simple at first glance, but i can't figure out how to make it nicely from IB. 我想要一些看起来很简单的东西,但我无法弄清楚如何从IB中做到这一点。

I want something simple, let's say i have two view into one parent (a view container that hold the two children). 我想要一些简单的东西,假设我有一个父视图(一个容纳两个孩子的视图容器)。 What i want is that they are each connected to vertical space at 0 px (one to his top the other to the bottom of the other). 我想要的是它们每个都连接到0 px的垂直空间(一个到另一个顶部,另一个到底部)。 So that when one of them change his size the other occupy the remainder size, and so there're will still be connected at 0 pixel each other. 因此,当其中一个改变其大小时,另一个占据剩余大小,因此仍将以0像素相互连接。

I've put two screenshoots of what i want. 我已经把我想要的两个屏幕截图。

在此输入图像描述在此输入图像描述

I've tried lot and lot, and nothing is working like i'm expecting. 我已经尝试了很多,并没有像我期待的那样工作。

What is strange that even if i put the priority at the vertical space, the other constraints take priority, and i just get a warning from XCode that my verticalSpace constraint is wrong. 奇怪的是,即使我把优先级放在垂直空间,其他约束优先,我只是从XCode得到一个警告,我的verticalSpace约束是错误的。

Could someone help me ? 有人能帮助我吗? I didn't find a specific tutorial on this kind of view managements. 我没有找到关于这种视图管理的具体教程。 Thanks 谢谢

* EDIT * *编辑*

Ok i figured out: I finally read the entire tutorial of Ray as suggested, and it helped me a lot (to my defense, i have read before the IOS official documentation about constraints, and it has confuse me more than helped me). 好吧我想通了:我终于阅读了Ray建议的整个教程,它对我帮助很大(为了我的辩护,我在IOS官方文档之前已经阅读过关于约束的内容,而且让我感到困惑的不仅仅是帮助我)。 To me, what was hard to understand is that now you can't change UIView by frame but now by constraint. 对我来说,难以理解的是,现在你不能按框架改变UIView,而是现在通过约束。 When I understood this, it was really easy to make the changes. 当我理解这一点时,很容易做出改变。

By the way i tried the @Handsomeguy's response, and it worked like a charm. 顺便说一句,我尝试了@ Handsomeguy的回应,它就像一个魅力。

I think you'll need at least one height constraint (with IBOutlet to your view controller) on one of your views... when you change its constant value, the other view should automatically change. 我认为你的一个视图至少需要一个高度约束(对你的视图控制器使用IBOutlet)......当你改变它的常量值时,另一个视图应该自动改变。 You can try on storyboard first, and then write down the logic. 您可以先尝试故事板,然后记下逻辑。 But without an height constraint on one of them I don't think it will ever work. 但是如果没有高度限制,我认为它不会起作用。

I was able to do it by setting constraints in interface builder: 我能够通过在界面构建器中设置约束来实现:

  1. vertical spacing: top layout guide -> yellow 垂直间距:顶部布局指南 - >黄色
  2. vertical spacing: yellow -> red 垂直间距:黄色 - >红色
  3. vertical spacing: red -> bottom layout guide 垂直间距:红色 - >底部布局指南
  4. height: yellow 身高:黄色

Set an outlet to the height constraint, then to change the height of the yellow view just use _height.constant = NEW_HEIGHT 将出口设置为高度约束,然后更改黄色视图的高度只需使用_height.constant = NEW_HEIGHT

You can probably omit the height constraint if the top view reports it's intrinsic content height appropriately. 如果顶视图适当地报告其内在内容高度,则可以省略高度约束。

Without auto layout it would be as easy as putting an anchor-bottom on the yellow view, and putting an anchor-top on the red one. 没有自动布局,就像在黄色视图上放置一个锚底,并在红色底部放置一个锚顶一样容易。 Now, when you change the parent view, the views would resize 50/50. 现在,当您更改父视图时,视图将调整为50/50。

Use autolayout. 使用自动布局。 You need the following constraints: 您需要以下约束:

both views pinned to left and right edge of containerView
redView top pinned to containerView top
yellowView top pinned to redView bottom
yellowView bottom pinned to containerView bottom
height constraint on **either* redView **or** yellowView!

This is very easy using this category: https://github.com/jrturton/UIView-Autolayout 使用此类别非常容易: https//github.com/jrturton/UIView-Autolayout

UPDATE UPDATE

I just noticed you said you wanted to do it in IB. 我刚注意到你说你想在IB做这件事。 You can use the same principle here, but I find it is much more convenient (and also easier to read later) if you use the category mentioned. 你可以在这里使用相同的原理,但如果你使用上面提到的类别,我发现它更方便(以后也更容易阅读)。 There will be very few lines of code, and the code will clearly show the intent. 将会有很少的代码行,代码将清楚地显示意图。

** UPDATE 2** **更新2 **

An explicit height constraint may not be necessary if the intrinsic size on one view is changed. 如果更改一个视图上的内在大小,则可能不需要显式高度约束。 This depends on what kind of views the yellow and green views are. 这取决于黄色和绿色视图的视图类型。

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

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