xcode 如何在自动布局约束中使项目全宽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32823380/
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
How to make an item full width in auto layout constraint?
提问by MonkeyBonkey
I'm trying to make a button full width in ios 9 storyboards, and am trying to create a width auto constraint, but the width constraint only seems to take an exact static number. How do I make the item go full width.
我试图在 ios 9 故事板中制作一个全宽的按钮,并试图创建一个宽度自动约束,但宽度约束似乎只采用一个精确的静态数字。如何使项目全宽。
Additionally if I wanted say an imageview to be full height and adjustable width to maintain it's aspect ratio - or a size to fit fill, how would I do that with the auto layout constraints in the storyboard.
此外,如果我想说一个图像视图是全高和可调宽度以保持其纵横比 - 或适合填充的大小,我将如何使用故事板中的自动布局约束来做到这一点。
Should I be doing this programmatically in the view controller?
我应该在视图控制器中以编程方式执行此操作吗?
回答by Kitson
For the button:
对于按钮:
In storyboard drag the left and right sides of the button to make it full width. Make sure the button is selected and add a pin constraint to the left and right (if you've dragged it full width the pin constraint should be equal to -20)
在情节提要中拖动按钮的左右两侧使其全宽。确保选择了按钮并向左右添加了一个引脚约束(如果您已将其拖到全宽,则引脚约束应等于 -20)
For the imageView:
对于图像视图:
The full height constraint is done almost exactly the same, although this time you need to drag the top and bottom to the top and bottom edges of the view controller and pin the top and bottom to their respective Layout guide.
全高度约束几乎完全相同,尽管这次您需要将顶部和底部拖动到视图控制器的顶部和底部边缘,并将顶部和底部固定到各自的布局指南。
You can make the image full screen in the view controller (pin top, bottom, left & right) and then edit the aspect ratio of the image in the Attributes inspector when the imageView is selected:
您可以在视图控制器中使图像全屏显示(固定顶部、底部、左侧和右侧),然后在选择 imageView 时在属性检查器中编辑图像的纵横比:
Hope that helps!
希望有帮助!