围绕在IOS使用自动布局邻居之间查看 [英] Centering View between neighbors using autolayout in IOS

查看:201
本文介绍了围绕在IOS使用自动布局邻居之间查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用从界面构建约束(自动布局)布局的屏幕设计,并希望避免编码的限制,但我会接受我猜这两种情况下的答案。

I am attempting to layout a screen design using constraints (auto-layout) from interface builder and hope to avoid coding constraints, but I would accept an answer for either case I guess.

我有一个包含在顶层视图两个子视图。我想解决的第一个子视图到屏幕顶部的垂直距离(我能实现这个 - 常见的情况对于垂直空间限制)。我想在第二子视图中第一子视图的底部,并在屏幕的底部之间的距离的中心(垂直地)浮动。我们的想法是,该设计有所响应到iPhone 3.5与4的尺寸。

I have two subviews in a contained in a top level view. I want to fix vertical distance of the first subview to the top of the screen (I was able to accomplish that - common use case for a vertical space constraint). I want the second subview to float (vertically) in the center of the distance between the bottom of first subview and the bottom of the screen. The idea is that the design responds somewhat to iPhone 3.5" vs 4" dimensions.

我有一个能够完成这个麻烦定义约束或约束集。

I am having trouble defining a constraint or set of constraints that would accomplish this.

我曾尝试在第二子视图和它的邻居(第一子视图)的顶部和上海华底部之间的垂直间距限制设置的不平等,并与这些约束的优先顺序播放。

I have tried setting inequalities on the vertical spacing constraints between the second subview and the top of it's neighbor (the first subview) and the bottom of the superview, and playing with the priorities of those constraints.

这建议在一些其他相关讯息在SO一​​个想法是使用被固定在屏幕的底部的容器,以及固定到所述第一子视图的底部边缘,然后居中我的第二子视图在容器中。我希望避免复杂视图层次结构如果有可能,但也许这是不容易避免的。

An idea that was suggested in a few other related posts on SO is to use a container that is pinned to the bottom of the screen, as well as the pinned to the bottom edge of the first subview, and then center my second subview in the container. I was hoping to avoid complicating the view hierarchy if possible, but maybe that isn't easily avoided.

任何想法或建议?

推荐答案

这个答案介绍如何在X code ++ 6.0做到这一点(和可能工作在X $ C $ ç5.0和5.1)。

UPDATE

This answer describes how to do this in Xcode 6.0 (and probably works in Xcode 5.0 and 5.1).

如果你想自动布局要做到这一点,你必须添加至少一个间隔视图。它不必是垂直中心的观点(或任何其他视图)的上海华,所以对你的看法层次结构中的pretty影响较小。

If you want to do this with auto layout, you have to add at least one spacer view. It doesn't have to be the superview of the vertically-centered view (or any other view), so it has a pretty minor effect on your view hierarchy.

我建议不要尝试设置在Interface Builder约束X $ C $Ç4.6.3。这只是太痛苦了。但如果你真的必须的,这是可行的。

I recommend not trying to set up constraints in Interface Builder in Xcode 4.6.3. It's just too painful. But if you really must, this is doable.


  1. 创建一个垫片(普通的老式的UIView )从上拥抱以上海华的底部边缘的底部边缘。将其设置为隐藏。给它约束顶端-拥抱视图和根视图的左边缘和底部边缘,并且针的宽度。我的间隔宽度为10:

  1. Create a spacer (a plain old UIView) from the bottom edge of the top-hugging view to the bottom edge of superview. Set it to hidden. Give it constraints to the top-hugging view and the left and bottom edges of the root view, and pin its width. My spacer width is 10:

添加您的中间视图(在这里,一个按钮)。给它一个水平居中集装箱的约束。然后,选择这两个中间的观点和看法间隔,给他们一个垂直居中的约束:

Add your middle view (here, a button). Give it a "Horizontal Center in Container" constraint. Then select both the middle view and the spacer view and give them a "Vertical Centers" constraint:

请注意,如果你把中间的视图时IB是显示了正确的指导方针,这将设置这些限制你。

Note that if you drop the middle view when IB is showing the correct guidelines, it will set these constraints for you.

您可以通过启用调整为后代(祖先但不是兄妹&AMP)测试。

You can test by enabling resizing for descendents (but not siblings & ancestors) and resizing the root view in IB. The middle view will remain centered between the top-hugging view and the bottom of the superview:

这篇关于围绕在IOS使用自动布局邻居之间查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆