问题描述
我知道汽车的布局,但我是新来自动排版普遍应用。我有问题设置约束
这下面的图片。
你能告诉我,我怎么设置自动布局通用应用程序?我试着用约束保证金和放大器;针龙头,顶部,尾随的空间容器,但它不能正常工作。
其实,自动布局工作比其他视图的滚动视图内不同。内部滚动视图,领先,落后,顶部和底部的制约形成的SuperView到容器视图(在这种情况下,滚动型)定义不间隔,而是一种我的滚动视图应该多少滚动到左,右,上和该组件的底部。
所以,在滚动视图中使用自动布局,你必须做一些小技巧:
- 设置前置,从你的滚动视图顶部和底部约束你的看法
该视图
当您打开该项目,请注意,对象和滚动视图之间的所有约束并不反映间距(我说),但反映了有多少你的滚动视图应该远离组件滚动。
希望这有助于
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:
- Set leading, trailing, top and bottom constraints from your Scrollview to your view
- 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.
- 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.
- 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组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!