自动布局定心鉴于剩余空间(编程) [英] Auto Layout centering view in remaining space (programatically)

查看:158
本文介绍了自动布局定心鉴于剩余空间(编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加自动布局约束的编程以居中的剩余空间视图(见下面的例子图)?

How can add Auto Layout constraints programatically to center a view in the remaining space (see example picture below)?

有关的那一刻,我在底部添加一个容器视图,然后我中心在容器视图的视图,但我想知道是否有无需使用容器视图任何其他解决方案。

For the moment, I add a container view at the bottom and I then center the view in the container view, but I was wondering if there was any other solution without needing to use a container view.

推荐答案

您需要添加一个垫片,以做到这一点。

You need to add a spacer view to do this.

让我们先从一些看法:

我会成立了粉红色视图占用根视图的前70%。首先,我将其固定到根视图的四边:

I'll set up the pink view to take up the top 70% of the root view. First I pin it to all four edges of the root view:

然后我会以两种方式编辑底部约束。首先,我要确保的第一个项目是粉红色的视图,第二我订的0.7倍增。然后,我更新了粉红色视图的框架:

Then I'll edit the bottom constraint in two ways. First, I make sure the first item is the pink view, and second I set the multiplier of 0.7. Then I update the pink view's frame:

接下来,我将添加间隔视图。我不想隔离视图在运行时可见的,所以我会隐藏它。隐藏的观点仍然参与布局。在设置的限制,我只是把间隔蓝视图左侧:

Next I'll add the spacer view. I don't want the spacer view to be visible at runtime, so I'll make it hidden. Hidden views still participate in layout. Before setting up constraints, I just put the spacer to the left of the blue view:

现在我将创建约束,使从粉红色视图根视图的底部,底部的间隔拉长。宽度并不重要,所以我只会将其固定到上海华的左边缘,使其薄:

Now I'll create constraints to make the spacer stretch from the bottom of the pink view to the bottom of the root view. The width doesn't matter so I'll just pin it to the left edge of the superview and make it thin:

现在我已经准备好组建蓝色视图。首先,我给它一个固定的大小:

Now I'm ready to set up the blue view. First I'll give it a fixed size:

其次,我将在根视图水平居中:

Second I'll center it horizontally in the root view:

第三,我会销其垂直中心间隔的垂直中心:

Third I'll pin its vertical center to the spacer's vertical center:

这就是我需要的所有限制。我会更新所有帧检查:

That's all the constraints I need. I'll update all frames to check:

我可以在助理编辑使用preVIEW测试一下:

I can test it out using Preview in the assistant editor:

注意,间隔观点并不在preVIEW可见,但仍参与布局。

Notice that the spacer view isn't visible in the preview, but still participates in layout.

这篇关于自动布局定心鉴于剩余空间(编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆