这里我有两个标签要在 Xcode 中显示。如果我不添加任何约束,它看起来不错。
无约束
在模拟器中显示
然后我需要自动布局,以便它可以在不同的设备中正常显示。但是一旦我添加了约束,他们就会将我所有的标签移到左板上。无论我如何设置,它都不起作用。
例如,一旦我为边框设置了一个恒定的前导空间,
只有一个约束,引导空间到内容,值 15
,
我的标签跳到了边界。
跳到边界。同样的事情发生在 textfield
这让我困惑了近三天。我怎么能让它工作?
更新:
I tried to add possible constrains, the constraint lines shown blue, but once I ran it, the label and textfield covered each other. I don't know if there are some important things I still lost?
When you have no constraints, iOS uses something known at the autoresizing mask to create constraints for you at runtime, locking your views in place wherever they were in Interface Builder. These constraints won't work well for different content sizes or different devices, so you should always fully constrain your UI.
When you added the single constraint, Interface Builder told you that your interface wasn't properly constrained (the red arrow next to your view controller) and would give you guidance if you looked at that content. However, running the app with only a single constraint on that label meant that the system didn't apply the autoresizing mask like normal and the layout system essentially just set all of the other values to zero at runtime. This includes the height
and width
of the label, as well as its x
and y
position. With zeros for all of those values, what you're seeing here is the content of your label being rendered at 0,0
, with a label that has 0 height and width. However, with clipsToBounds
defaulted to false
, the label's content is still visible, and so is shown as you see.
tl;dr:正如 Interface Builder 试图告诉您的那样,您需要限制整个 UI。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句