xcode 在自动布局 (iOS) 中将一个项目置于其他两个项目之间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32619855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Center an item between two other items in autolayout (iOS)
提问by Josh O'Connor
I have 5 items on the top of my View Controller, and I am trying to space them evenly with autolayout. For the left item (back button), I added a constraint to be 15px from the left border, and for the right item (camera), I added a constraint to be 15px from the right border. Then, I set up the center item (0:01) to align center x so it is in the middle. Lastly, I selected "Reset to suggested constraints" for the ... and the flash item.
我的视图控制器顶部有 5 个项目,我正在尝试使用自动布局将它们均匀分布。对于左侧项目(后退按钮),我添加了距离左边框 15px 的约束,对于右侧项目(相机),我添加了距离右侧边框 15px 的约束。然后,我设置中心项 (0:01) 以对齐中心 x 使其位于中间。最后,我为 ... 和 flash 项目选择了“重置为建议的约束”。
Upon looking at it in the iphone 5 and 4, it looks great. As you can see, all the items are evenly divided:
在 iphone 5 和 4 中查看它时,它看起来很棒。如您所见,所有项目均等分配:
So far, everything is going smoothly. HOWEVER, when I test on the iphone6 and iphone6+, the "..." and the flash symbol are not centered between the < and the 0:01, as depicted:
到目前为止,一切都很顺利。但是,当我在 iphone6 和 iphone6+ 上进行测试时,“...”和闪光符号不在 < 和 0:01 之间居中,如下所示:
How do you center an item between two neighboring items? Aka, how to I center the "..." to be exactly in between the < and 0:01?? This simple task is taking me hours, and I don't understand why Xcode doesnt have a center between two neighbors.
如何将一个项目置于两个相邻项目之间?又名,如何将“...”集中在 < 和 0:01 之间?这个简单的任务花了我几个小时,我不明白为什么 Xcode 在两个邻居之间没有中心。
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by picciano
The only way I have figured out how to do this is to create a container that holds the thing you want to center between the two items. That container should have the same trailing and leading amounts and then just center your "thing" in that container.
我想出如何做到这一点的唯一方法是创建一个容器,其中包含您想要在两个项目之间居中的东西。该容器应该具有相同的尾随和前导数量,然后将您的“东西”放在该容器中。
In the example below, the blue box would represent your orange "..." and the dark grey box would represent the container just used for centering. Once the layout works, just set the container to have a clear fill.
在下面的示例中,蓝色框将代表您的橙色“...”,而深灰色框将代表仅用于居中的容器。布局工作后,只需将容器设置为清晰填充即可。
回答by Uwe
Just to add: If you use UIImageView as container, then it is already transparent as you don't populate it with an image. Also, the top and bottom handles are easy to position
补充一点:如果你使用 UIImageView 作为容器,那么它已经是透明的,因为你没有用图像填充它。此外,顶部和底部手柄易于定位