我何时必须在iOS AutoLayout中使用优先级?

我何时必须在iOS AutoLayout中使用优先级?,第1张

概述使用iOS AutoLayout,您将执行以下 *** 作: self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 0.3, 使用iOS autoLayout,您将执行以下 *** 作:

self.vIEw.addConstraint(NSLayoutConstraint(item: label,attribute: .WIDth,relatedBy: .Equal,toItem: self.vIEw,multiplIEr: 0.3,constant: 0))

您还可以为每个布局约束使用优先级.

我何时必须在iOS autoLayout中使用优先级?

解决方法 您可以使用它来近似条件逻辑.

例如,我有一个裁剪视图(想象一个面部轮廓的圆形切口).如果图像是肖像,我希望圆圈靠近顶部,但如果图像是横向的,那么就没有空间让它偏离中心,这是没有意义的.

为了解决这两种情况,我首先设置边界约束 – 叠加应始终在图像视图内(使用< = /> =来自边缘的内容的约束).然后我有一个较低优先级的另一个约束,说理想情况下它会高于中心,但是低于那个说法的后备约束,否则我希望它居中.最后两个约束导致冲突,但布局引擎选择最高优先级.在风景情况下,上面的中心是不可能的,所以AL打破了它并且留下了居中的约束.

另一个例子 – 也是下面的代码片段中的一个例子是,我希望我的叠加层尽可能多地使用空间,同时保持适当的宽高比.这意味着虽然我的外部约束是绝对的,但我宣称理想的裁剪框架位于边界,知道当时只能满足一组,但宽高比优先.然后,AL会在尽可能多的情况下打破 – 在每个方向上都是两个,而另外两个则是机智的.没有它,就会有许多可能的解决方案.

在我的实际代码中,我将这些作为一组约束捕获.每个由一个惰性实例变量创建:

self.addConstraints([        // absolute constraints        self.leftmarginConstraint,self.rightmarginConstraint,self.topmarginConstraint,self.bottommarginConstraint,self.aspectRatioConstraint,self.constrain(self.maskContainer,self,.CenterX,.Equal),// hug the edges as much as possible,but only two of the four will be used        self.constrain(self.maskContainer,.left,.Equal,priority:900),self.constrain(self,self.maskContainer,.Right,.top,.Bottom,// try for above center,but accept vertically centered        self.constrain(self.maskContainer,.CenterY,priority:800,multiplIEr:0.8),priority:500),])
总结

以上是内存溢出为你收集整理的我何时必须在iOS AutoLayout中使用优先级?全部内容,希望文章能够帮你解决我何时必须在iOS AutoLayout中使用优先级?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1027188.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存