简体   繁体   English

故事板上的UIStackView强制固有内容的大小

[英]UIStackView in storyboard forces intrinsic content size

In storyboard I have added a UIStackView to a UIView and inside the UIStackView I've added a UIImageView with an image specified. 在情节提要中,我已将UIStackView添加到UIView中,并且在UIStackView中添加了具有指定图像的UIImageView。 The image specified is in XCAssets and has a 2X size of 59X60. 指定的图像位于XCAsets中,其2X大小为59X60。 The UIImageView is getting its width set to this intrinsic size but I want it to be 44X44 and the image to scale to fit. UIImageView将其宽度设置为此固有大小,但我希望它的大小为44X44,并且图像要缩放以适合。 However in Storyboard with and Height settings are disabled and set to 59X60 for the ImageView and for the UIStackView as well. 但是,在Storyboard中,with和Height设置被禁用,并且ImageView和UIStackView的设置都设置为59X60。 I want to have 5 of these same ImageViews with stars stack horizontally at 44X44 inside the UIStackView. 我想在UIStackView中以44X44的水平方向堆叠星号的5个相同ImageViews。

在此处输入图片说明

Ideas? 想法?

Update: It seems that adding a height constraint to the UIStackView will cause the scaling but the star is then distorted in appearance. 更新:似乎向UIStackView添加了高度限制会导致缩放,但是星形会变形。

The width and height fields are grayed out because the image view is inside a stack view. 宽度和高度字段显示为灰色,因为图像视图位于堆栈视图内部。 You must use constraints to control the size of a stack view's arranged subviews. 您必须使用约束来控制堆栈视图的排列子视图的大小。

First, make sure all five star views have “Content Mode” set to “Aspect Fit”: 首先,确保所有五个星形视图的“内容模式”都设置为“纵横比”:

将内容模式设置为适合方面

Then, constrain all five star views to have equal width and height to each other: 然后,将所有五个星形视图约束为彼此相等的宽度和高度:

宽度和高度相等

Finally, constrain one of the star views to have a constant width of 44 and a constant height of 44: 最后,将其中一个星形视图约束为具有恒定的宽度44和恒定的高度44:

恒定的宽度和高度约束

If you want space between the stars, set the “Spacing” of the stack view: 如果要在星星之间留出空间,请设置堆栈视图的“间距”:

堆栈视图间距

If you want padding around the stars, you can do that outside the stack view, or you can set the stack view's “Layout Margins” to fixed constants (and turn off “Safe Area Relative Margins”): 如果要在星星周围进行填充,可以在堆栈视图之外执行此操作,也可以将堆栈视图的“布局边距”设置为固定常数(并关闭“安全区域相对边距”):

堆栈视图布局边距

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM