Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

ios - 自动布局和线性方程(Autolayout and linear equation)

I'm trying to push my iOS development learning a little bit further by taking a look into auto layout and linear equation y = mx + b .

(我试图通过研究自动布局和线性方程y = mx + b进一步推动我的iOS开发学习。)

Here is a simple exercise that I'm using : I have the SafeArea View and I added a greeView just above it.

(这是我正在使用的一个简单练习:我有SafeArea View并在其上方添加了greeView 。)

and I marked the four constraints that I'm interested in (#1 for the the top constraint, #2 for the Trailing one, #3 for the bottom one and #4 for the Leading constraint)

(并标记了我感兴趣的四个约束(#1代表顶部约束,#2代表尾随约束,#3代表底部约束,#4代表Leading约束))

在此处输入图片说明

So, by applying the linear equation for the first one for example, it would be :

(因此,例如,通过对第一个方程应用线性方程,将是:)

(1) : greenView.Top = 1.safeArea.top + 20

((1):greenView.Top = 1.safeArea.top + 20)

but apparently for the second constraint it's not :

(但显然对于第二个约束不是:)

(2) : greenView.Trailing = 1.safeArea.Trailing + 20

((2):greenView.Trailing = 1.safeArea.Trailing + 20)

but instead, it should be :

(但是,应该是:)

(2) : safeArea.Trailing = 1.greenArea.Trailing + 20

((2):safeArea.Trailing = 1.greenArea.Trailing + 20)

I think I'm missing something to understand the logic behind the alignment of the 2nd constraint.

(我想我缺少一些了解第二约束对齐方式背后的逻辑的东西。)

Any hints please ?

(有什么提示吗?)

Thank you

(谢谢)

  ask by Hajar ELKOUMIKHI translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There need to be some implicit order for the flow of constraints so that you know what "before" and "after" means.

(约束流需要有一些隐式顺序,以便您知道“之前”和“之后”的含义。)

Apple has chosen top to bottom, leading (left in a LTR locale) to trailing (right).

(苹果公司选择了从上到下,领先(在LTR语言环境中为左)到尾随(右)。)

You want the trailing edge of the green view to be 20 "before" the trailing edge of the superview.

(您希望绿色视图的后沿在超级视图的后沿“之前”为20。)

There are two ways to specify this;

(有两种指定方法;)

as you have in (3) where the constraint says that the superview trailing is 20 more than the green trailing.

(就像您在(3)中所说的那样,约束条件表明超级视图的尾部比绿色的尾部多20。)

You could also say greenView.trailing = 1.safeArea.trailing-20 to get the result you want.

(您也可以说greenView.trailing = 1.safeArea.trailing-20以获取所需的结果。)

If you have the constraint as per (2) then the green view would be 20 off the right edge of the screen.

(如果您具有(2)的约束,则绿色视图将偏离屏幕右边缘20。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...