ios autolayout 使用总结

来源:互联网 发布:全球数据公司排名 编辑:程序博客网 时间:2024/05/29 03:39
iOS6中引入了AutoLayout技术。

Content Hugging 以及 Content Compression Resistance ,这两个属性可应用于intrinsic content size的控件。 例如button,label。 intrinsic content size官方的解释:

Custom views typically have content that they display of which the layout system is unaware. Overriding this method allows a custom view to communicate to the layout system what size it would like to be based on its content. This intrinsic size must be independent of the content frame, because there’s no way to dynamically communicate a changed width to the layout system based on a changed height, for example.

Hugging priority 确定view有多大的优先级阻止自己变大。

Compression Resistance priority确定有多大的优先级阻止自己变小。

Content Hugging Priority:内容压缩优先级,默认为250

Content Compression Resistance Priority:阻止内容压缩优先级,默认为750


1.Compression Resistancepriority的使用

A、对一个label指定字体和内容设置上左右约束,然后分别修改其左右约束的priority数值观察效果。

    _RConstraint.priority =750;

    _LConstraintt.priority = 750;

    label的属性数值。

   这种情况会先满足label的左右约束,再去满足Content Compression Resistance ,label可能被压缩。

B、

    _RConstraint.priority = 750;

    _LConstraintt.priority = 749;

    label的属性数值。

   这种情况会先满足Content Compression Resistanceabel不会被压缩再去满足label的右约束 ,最后考虑左约束(其实感觉已经失去意义),label可能显示不全,x初始值可能为负数,label不会被压缩。

C、

    _RConstraint.priority = 749;

    _LConstraintt.priority = 750;

    label的属性数值。

   这种情况会先满足Content Compression Resistance【abel不会被压缩,再去满足label的左约束 ,最后考虑右约束(其实感觉已经失去意义),label可能显示不全,x+width值可能大于界面大小

2.Content Hugging  priority的使用

A、对一个label指定字体和内容设置上左右约束,然后分别修改其左右约束的priority数值观察效果。

  

    _RConstraint.priority = 250;

    _LConstraintt.priority = 251;

这种情况抗拉伸优先相同,label未被拉伸,左侧约束正常,右侧约束失去效果,被拉伸。

B、

  

    _RConstraint.priority = 251;

    _LConstraintt.priority = 250;

这种情况抗拉伸优先相同,label未被拉伸,右侧约束正常,左侧约束失去效果,被拉伸。

C、

  

    _RConstraint.priority = 250;

    _LConstraintt.priority = 250;

这种情况抗拉伸优先相同,label被拉伸,左右侧约束正常。

3. 对于两个控件并排布局使用  Content Compression Resistance 、Content Hugging  priority。

并排使用两个空间,相应的左右约束可以更改优先级,两个控件也可以设置拉伸压缩优先级,具体的使用还是参考需求对应修改吧。

例如,两个按钮水平布局 ,右侧的按钮保持不拉伸不压缩,那么只需把左侧的Content Compression Resistance 、Content Hugging  priority弄小点就行了。


////后续继续补充。



1 0
原创粉丝点击