简体   繁体   English

如何在UIStackView内部垂直和水平居中

[英]How to center both vertical and horizontal inside a UIStackView

I have a simple UIImageView with 50x50 size . 我有一个50x50大小的简单UIImageView If I had a UIView of size 100x100 and I wanted to center the image inside of it, all I have to do is set these 如果我有一个大小为100x100的UIView并且我想将图像置于其中心,我所要做的就是设置这些 在此输入图像描述

But how do I do the same with a UIStackView of size 100x100 ? 但是我如何使用尺寸为100x100UIStackView做同样的事情呢?

EDIT i think i should have been more clear. 编辑我想我应该更清楚。 The questions is, how do i center the image both vertically & horizontally inside of the UIStackview ? 问题是,如何在UIStackview内部垂直和水平居中显示图像?

Use two stack views. 使用两个堆栈视图。

Vertical stack view with centre alignment. 垂直堆栈视图与中心对齐。

Inside that is a horizontal stack view with centre alignment t. 内部是具有中心对齐t的水平堆栈视图。

Inside that is your image. 里面就是你的形象。

You need to set the alignment to center inside attribute inspector of UIStackView 您需要将对齐设置为UIStackView属性检查器中的UIStackView

在此输入图像描述

Check attribute inspector for stackview settings: 检查属性检查器的stackview设置:

图片

follow these steps. 跟着这些步骤。

1) drag Uiimageview. 1)拖动Uiimageview。

在此输入图像描述

2) click on UIimageview. 2)单击UIimageview。 Press Ctrl and drag downward or sidewise to parent view. 按Ctrl键并向下或向侧面拖动到父视图。 在此输入图像描述

3) click on "centre vertically in view" and centre "horizontally in view" 3)点击“在视图中垂直居中”并居中“水平在视野中”

4) then set height and width like below. 4)然后设置高度和宽度,如下所示。 在此输入图像描述

Thats it. 而已。 your imageview will be at centre regardless of the device type. 无论设备类型如何,您的imageview都将位于中心位置。 good luck. 祝好运。

leave a comment if you any question. 如果您有任何疑问,请发表评论。 Accept the answer if it helps you. 如果有帮助,请接受答案。

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

相关问题 Swift 如何以编程方式将垂直 UIStackview / UIView 放入水平 UIStackview - Swift how to put vertical UIStackview / UIView inside horizontal UIStackview programatically 水平的UIStackView-如何在内部对齐项目,以便一个向左浮动而另一个在中心对齐? - Horizontal UIStackView - how to align items inside so that one is floated left and the other sticks in center? 垂直 UIStackView:如何在元素之间保持相同的空间并将它们居中? - Vertical UIStackView: how to keep same space between elements and center them? UIScrollView中的水平UIStackView不滚动 - Horizontal UIStackView inside UIScrollView not scrolling 如何同时使用水平和垂直滚动视图(快速) - How to work with both a horizontal & vertical scrollview (swift) 从中心对齐水平 UIStackView 中的项目 - Align items in horizontal UIStackView from the center UIScrollView中的UIStackView:如何居中对齐堆栈视图/项目? - UIStackView inside UIScrollView: how to center-align stack view / items? 如何在uistackview中水平居中放置项目 - How can I center items horizontally inside uistackview 如何在水平UIPageViewController中实现垂直UIPageViewController - How to implement a vertical UIPageViewController inside a horizontal UIPageViewController 如果视图位于UIStackView内部,则中心点错误 - Wrong center point if view is inside UIStackView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM