如何使用自动布局将 UILabel 设置为 UICell 宽度的百分比 [英] How to set a UILabel as a percentage of the width of the UICell with autolayout

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

问题描述

我想知道如何将 UILabel 的宽度设置为单元格总宽度的百分比?我正在使用自动布局.通常我会单击标签并将 EqualsWidth 设置为 parentView,然后调整乘数,因此我将宽度设置为 parentWidth 的百分比.但是使用 InterfaceBuilder 该选项似乎在 Cell 中不可用.

I'm wondering how to set a UILabel's width expressed as a % of the Cell's total width? I'm using AutoLayout. Normally I would click on the label and set the EqualsWidth to a parentView and then adjust the multiplier so I set the width as a % of the parentWidth. But using InterfaceBuilder that option does not seem to be available within the Cell.

但是我想知道它可能与设置首选宽度有关:我正在使用 ios8+ 并且已经读到隐式宽度不适用于 ios8?

However I'm wondering maybe it has something to do with setting the preferred Width: I'm using ios8+ and have read that implicit widths are not available for ios8?

我希望实现的是标签 1 和标签 3 都是单元格宽度的 15%,标签 2 的前缘和后缘固定在 UIImageView 和标签 3 上,这将允许它根据设备的宽度.

What I am hoping to achieve is that Label 1 and Label 3 are both 15% the width of the Cell and Label 2 has leading and trailing edges pinned to UIImageView and Label 3 that will allow it to grow and shrink depending on the width of the device.

推荐答案

为实现在单元格内设置任何子视图的百分比宽度的目标应该执行的步骤:

Steps you should do to achieve your goal of setting percentage width of any subview inside cell:

  1. 将 UIView 添加到您的单元格并将其固定到每个边缘 - 我们必须添加自定义容器视图,以便以后能够添加等宽"引脚.

  1. Add UIView to your cell and pin it to every edge - we have to add custom container view in order to be able to add "Widths equally" pin later on.

将 UILabel 添加到之前添加的 UIView 容器中.假设我们要添加左侧标签,因此添加 3 个引脚:左侧、顶部、底部.

Add UILabel to previously added UIView container. Lets say we want to add left side label, so add 3 pins to it: left, top, bottom.

您现在可以看到您的约束设置有错误.所以让我们修复它并添加宽度相等"引脚.在 View Controller Scene 菜单中选择 2 个视图 - 容器和标签.转到 XcodeMenu/Editor/Pin/并选择宽度相等".现在您应该在约束设置中看到没有错误,并且标签应该与容器具有相同的大小.(请注意,有时您必须在设置约束后刷新视图框架,所以如果您看到黄色"警告图标不要害怕,只需点击它即可.

You can see now that you have error in your constraint setup. So lets fix it and add "Widths equally" pin. Select 2 views in View Controller Scene menu - the container and the label. Go to XcodeMenu/Editor/Pin/ and select "Widths equally". Now you should see no errors in you constraints setup and the label should have same size as the container. (Please note that sometimes you have to refresh frames of your views after setting constraints, so if you see "yellow" warning icon don't be afraid and just click on it).

现在,如果您有所有限制,我们需要设置实际百分比值.打开选项卡 Size Inspector,单击步骤 3 中添加的约束上的编辑"标题.并将乘数值更改为 0.15 (15%).

Now if you have all your constraints we need to set actual percentage value. Open tab Size Inspector, click "Edit" title on constraint added in step 3. and change Multiplier value to 0.15 (15%).

请注意,如果您要使用相同的技术添加标签但右侧,则需要将乘数设置为 6.66 (= 1.00/0.15).

Please note that, if you would add label using same technique but for right side, then you would need to set Multiplier value to 6.66 (= 1.00 / 0.15).

干杯!

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

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