使用自动布局居中放置UIView只是不起作用

使用自动布局居中放置UIView只是不起作用

本文介绍了使用自动布局居中放置UIView只是不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UIImageView ,它的大小与屏幕尺寸相同(对于所有邻居,自动布局均为0).在其中,我还有另一个 UIImageView.,我希望它始终位于其父 imageview 的中心(因此位于屏幕的中心),因此会根据屏幕尺寸更改其尺寸,并保持宽高比.

I have UIImageView which is in the screen size (auto layouts are 0 to all neighbours) .Inside it, i have another UIImageView. , i would like it to always be in the Center of its parent imageview(hence on the Center of the screen) , and so it will change its size in relation to screen size, and keep aspect ratio .

因此,在 storyboard (Xcode 6)内部,我试图控制从中心"视图拖动到其父视图,而不是添加纵横比约束,这比我看到的要大.其长宽比,并增加了约束条件.

So inside the storyboard(Xcode 6), i am trying to control drag from the Center view to its parent, than i add aspect ratio constrain ,than i see its aspect ratio,and that the constrain added.

现在运行此操作,将更改图像大小,并且在所有屏幕上都保持相同大小.

Running this now, will NOT change image size and it stay in the same size for all screens.

以相同的方式,我想将其居中,因此我控制将其拖动到其超级视图,而不是选择 Center X Center Y ,不用说居中显示在任何屏幕尺寸上.

Than in the same way , i want to Center it , so i control drag to its superview , than chose Center X and Center Y , needless to say that its not centred to any screen size.

为什么要在中心放置视图如此困难?

Why is it has to be so hard to place a view in the Center ?

推荐答案

这非常容易.查看我的图片:

It's super easy. See my images:

然后您需要宽度/高度限制:

Then you need width/height-constraints:

您完成了!

要根据约束替换情节提要中的视图,只需选择视图本身并使用更新框架:

To replace your view in storyboard according to constraints, just select the view itself and use update frames:

如果您想要宽高比的宽度/高度,只需执行以下操作(乘数部分):

If you want aspect-ratio width/height, just do this (The multiplier part):

说明如何打开第一个窗口.然后只需按下复选框以添加约束:

Clarification of how to open the first window. Then just press the checkboxes to add the constraints:

这些容器中的中心"约束应与此窗口中添加的约束相同(按住CTRL键并拖动以打开超级视图以打开此约束):

These "center in container"-constraints should be the same as the added ones in this window (CTRL-drag to superview to open this):

要阐明要按下的按钮:

这篇关于使用自动布局居中放置UIView只是不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:11