本文介绍了通用自动布局了滚动和放大器; UI组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道汽车的布局,但我是新来自动排版普遍应用。我有问题设置约束这下面的图片。

你能告诉我,我怎么设置自动布局通用应用程序?我试着用约束保证金和放大器;针龙头,顶部,尾随的空间容器,但它不能正常工作。


解决方案

其实,自动布局工作比其他视图的滚动视图内不同。内部滚动视图,领先,落后,顶部和底部的制约形成的SuperView到容器视图(在这种情况下,滚动型)定义不间隔,而是一种我的滚动视图应该多少滚动到左,右,上和该组件的底部。

所以,在滚动视图中使用自动布局,你必须做一些小技巧:


  1. 设置前置,从你的滚动视图顶部和底部约束你的看法


  • 一个子视图添加到滚动视图,你会为指导视图中使用。由于从设备到设备滚动视图宽度的变化,您需要有滚动视图宽度的关系的图,所以你可以设置你与该视图,而不是滚动视图的左右边框(约束,因为这将决定不是大小,但多少你的滚动视图滚动向左或向右)。该视图的高度= 0,位于0,0,并具有相同的宽度与滚动视图。



  • 该视图
  • 设置限制。身高= 0领先的空间容器中,尾随的空间容器设置为0(这将告诉您的滚动视图不滚动到你的向导视图的两侧)。顶级约束添加到滚动视图也一样,因为你的指南查看将在滚动视图的顶部。还添加滚动视图,为向导视图之间的宽度相等的关系(这是非常重要的,这将迫使你的指导,以具有相同的宽度为滚动视图,因此没有水平滚动将可用)。请注意,在这一点上,你的滚动视图知道它必须多少滚动到左,右,和顶部,而不是底部,所以你会在自动版式看到一个错误。


  • 现在,你建立你的UI,考虑到顶部,左,右的关系必须放在关系导向视图,而不是滚动视图(要定义的间距,而不是滚动的量)。很难把这里所需要的所有约束,所以我在GitHub上创建了一个例子项目:

  • 当您打开该项目,请注意,对象和滚动视图之间的所有约束并不反映间距(我说),但反映了有多少你的滚动视图应该远离组件滚动。

    希望这有助于

    I know auto layout but I am new to universal application with auto layout. I am having problem setting up Constrain to this below image.

    Can you tell me how do I set auto layout for Universal App? I tried with constrain to margin & pin the leading,top,trailing space to container but it does not work.

    解决方案

    Actually, autolayout works different inside a scrollview than in other views. Inside a scroll view, the leading, trailing, top and bottom constraints form a superview to the container view (scrollview in this case) defines not spacing, but sort of "how much my scroll view should scroll to the left, right, top and bottom of this component".

    So, to use autolayout in a scroll view, you must do some tricky things:

    1. Set leading, trailing, top and bottom constraints from your Scrollview to your view
    1. Add a subview to the scrollview, that you'll use as a guide view. Since scrollview width changes from device to device, you need a view that have a relationship with the width of the scrollview, so you can set your constraints related to that view instead of the scrollview's left and right borders (since this will define not the size, but "how much your scrollview scrolls to left or right"). This view have a height = 0, is located in 0,0, and have the same width as the scrollview.
    1. Set constraints for that view. Height = 0. Leading space to container, and Trailing space to container set to 0 (this tells your scrollview not to scroll to the sides of your guide view). Add a top constraint to the scrollview too, since your guide view will be at the top of the scrollview. Also add a Equal width relationship between your scrollview and your guide view (this is very important, it will force your guide view to have the same width as the scrollview, hence no horizontal scroll will be available). Note that at this point, your scrollview knows how much it must scroll to the left, right, and top, but not to the bottom, so you'll see an error in the autolayout.
    1. Now you build your UI, considering that the top, left and right relationships must be put in relationship to the guide view, NOT to the scrollview (you want to define spacing, not "amount of scroll"). It's hard to put all constraints needed here, so I created an example project on github: Download it here

    When you open the project, please note that all constraints between an object and the scrollview doesn't reflect spacing (as I said) but reflects how much your scrollview should scroll away from the component.

    Hope this helps,

    这篇关于通用自动布局了滚动和放大器; UI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-02 18:24