UILabel文本属性设置为nil或“"让UILabel从视图中消失(Swift / Autolayout / iOS9.1) [英] UILabel text property when set to nil or "" makes UILabel disappear from view (Swift / Autolayout/ iOS9.1)

查看:405
本文介绍了UILabel文本属性设置为nil或“"让UILabel从视图中消失(Swift / Autolayout / iOS9.1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在参加斯坦福冬季2015 Swift / iOS课程,在完成作业时,我遇到了一个我想改变的行为。

I am going through the Stanford Winter 2015 Swift/iOS course and while doing the assignments I run into a behavior I'd like to change.

我使用的是Autolayout如视频中所述(将显示引脚设置为前导和尾随视图边缘)和计算器应用程序显示UILabel很好,初始值为0,并且每当用于设置它的值(字符串)为非零并且非。

I use Autolayout as described in the videos (making the display pin to leading and trailing view edges) and the Calculator app "Display" UILabel is fine with an initial value of 0 and whenever the value used to set it (a String) is non-nil and non "".

如果是nil或,则整个UILabel消失。我想要做的是在没有值显示时清除显示,或者错误的计算结果为零。

If it is either nil or "", the entire UILabel disappears. What I am trying to do is to "clear" the display whenever there is no value to display or an incorrect calculation resulted in nil.

任何关于谁应该处理的提示这一般吗? 清除UILabel而不更改它的屏幕尺寸?

Any tips on who to deal with this in general? "Clearing" a UILabel without changing it's on-screen dimensions?

编辑(谢谢Rob)
UILabel具有以下约束条件
1.按住Option键并单击向左拖动到包含UIView,选择引导某些东西(在通勤上班时无法检查确切的措辞。
2.方法与(1)相同)除了拖动到右边缘并选择尾随
3.选项点击 - 向上拖动到视图顶部,选择垂直菜单选项。
4.与(3)相同除外拖动到GUI上UILabel下面的UIButton。

Edit (thanks Rob) The UILabel has the following constraints 1. Option-click drag-left to containing UIView, selected "leading" something (on commute to work can't check yet for exact wording. 2. Same method as (1) except that the drag is to the right edge and selecting "trailing" 3. Option click-drag up to top of view, select "vertical" menu option. 4. Same as (3) except that drag is to a UIButton underneath the UILabel on the GUI.

使用这些设置时,包含数字的标签始终可见(如果理解,将为其着色)即使文本没有,也要在屏幕上伸展。

With those settings, the label when it contains a number is always visible and (if understand, will color it to verify) stretches across the screen even if the text doesn't.

只要UILabel的内容不为空,布局在配置文件和横向上看起来都是正确的。如果为空,它似乎缩小到适合,下面的按钮向上移动。

The layout looks correct in profile and landscape as long as content of UILabel is not empty. If empty, it seems to "shrink to fit" so much that the buttons below get moved up towards the top.

我是一个C ++开发人员从90年代中期开始,但我在iOS / Swift开发方面经验不多,只有几周的经验。

I'm a C++ dev since mid 90s but I have little UI experience and no more than a couple weeks experience in iOS/Swift development.

谢谢!

推荐答案

您始终可以为UILabel指定最小宽度和最小高度或约束,以保持标签的左侧和右侧。这应该使标签不会将其尺寸更改为零。

You can always give the UILabel a min width and min height or constraints that holds the left and right side of the label. That should keep the label from changing it's dimensions to zero.

这篇关于UILabel文本属性设置为nil或“"让UILabel从视图中消失(Swift / Autolayout / iOS9.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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