如何使用自动布局创建总宽度的百分比? [英] How to create percentage of total width using autolayout?

查看:26
本文介绍了如何使用自动布局创建总宽度的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建三个动态列,每个列都占总宽度的固定百分比.不是三分之一,而是不同的价值观.例如,下图显示了三列:第一列宽 42%,第二列宽 25%,第三列宽 33%.

I need to create three dynamic columns, each with a fixed percentage of the total width. Not thirds, but different values. For example, the following illustration shows three columns: the first being 42% wide, the second being 25% wide, and the third being 33% wide.

对于跨视图控制器的 600 像素,分别为 252、150 和 198 像素.

For a 600 pixel across viewcontroller, that would be 252, 150, and 198 pixels respectively.

但是,对于任何后续的显示尺寸(即 iPhone 4 横向(960 宽)或 iPad 2 纵向(768 宽),我希望相对百分比相同(而不是上面引用的像素宽度).

However, for any subsequent display sizes (i.e. iPhone 4 landscape (960 wide) or iPad 2 portrait (768 wide), I would like the relative percentages to be the same (not the pixel widths quoted above).

有没有办法使用故事板(即没有代码)来做到这一点?我可以在代码中轻松完成此操作,但我的目标是将尽可能多的显示逻辑放入 Storyboard.

Is there a way to do this using Storyboards (i.e. without code)? I can do this easily in code, but my goal is to put as much of this display logic as possible into the Storyboard.

推荐答案

如果,如您所说,您知道如何在代码中进行操作,那么您已经知道如何在故事板中进行操作.这是完全相同的约束,但您是在视觉上而不是在代码中创建它们.

If, as you say, you know how to do it in code, then you already know how to do it in the storyboard. It's exactly the same constraints, but you are creating them visually rather than in code.

  1. 选择一个视图及其超级视图.

  1. Select both a view and its superview.

Choose Editor -> Pin -> Widths Equally 将宽度限制为与超级视图的宽度相等(实际上画布底部的pin"弹出对话框在这里效果最好).

Choose Editor -> Pin -> Widths Equally to constrain the width to be equal to the superview's width (actually the "pin" popup dialog at the bottom of the canvas works best here).

编辑约束并将乘数设置为所需的分数,例如0.42.其他视图也是如此.

Edit the constraint and set the Multiplier to the desired fraction, e.g. 0.42. And so too for the other views.

这篇关于如何使用自动布局创建总宽度的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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