Understanding “firstBaseLine” vs. “baseLine” AutoLayout Constraint

最后都变了- 提交于 2019-12-21 07:23:22

问题


I've created the following view that looks as follow:

And used Add missing constraints t automatically add constraints. I am pretty new in iOS world and I'm trying to configure out how constraints work.

On document outline, I can see the added constraints:

I click on label UR and Size inspector show me following:

What is the difference between constraints aligned by first baseline to UL and aligned by baseline to UL? When I click on the size inspector on constraints button, it shows me the same line.


回答1:


From the Apple Docs:

NSLayoutAttributeBaseline
The object’s baseline.
Available in iOS 6.0 and later.
NSLayoutAttributeFirstBaseline
The object’s baseline. For objects with more than one line of text, this is the baseline for the topmost line of text.
Available in iOS 8.0 and later.

Here is also a screenshot of the difference when aligning to a three line label. The upper green label is using NSLayoutAttributeFirstBaseline and the lower green label is using NSLayoutAttributeBaseline. These are the only constraints on the two green labels besides they are both Horizontally pinned 40 from the Pink Label




回答2:


I don't recommend ever using "Add Missing Constraints." It is a very unpredictable wizard that DWIW (does what it wants). Learn each constraint and add it by hand. Ctrl-drag between objects in the scene hierarchy; use the constraints inspector and edit numeric values; use 'Update Frames' from the triangle pop-up; delete constraints from the inspector; repeat until you get it.

firstBaseLine refers to the top baseline of a multi-line object: Storyboard warning : First baseline layout attribute before iOS 8.0

if the label or text field involved only has one line, it's a useless constraint.

When you want two side-by-side one-line blotches of text to align vertically, even though their font sizes, ascenders, and baseline descenders may differ, use baseLine alignment. It will look natural like the strings are on the same line.

Bottom, Top, and Center vertical alignments are also possible and more appropriate for non-text objects.



来源:https://stackoverflow.com/questions/36188488/understanding-firstbaseline-vs-baseline-autolayout-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!