什么是默认的自动布局内容拥抱和内容压缩阻力优先级值?

我试图debugging自动布局问题,并知道内容拥抱和内容压缩阻力优先级的默认值将有所帮助。

他们是什么? 它们是特定于特定组件吗? 有什么常数我可以用来引用他们?

关键字:UILayoutPriority

这些值是Xcode 7.3中的最新值。


界面生成器默认优先级值:

将组件拖到Interface Builder中时,会得到以下优先级。 注意: (250,750)代表250水平和750垂直。

 +-------------------------+---------------+------------------------------+ | Object | Hugging (H,V) | Compression Resistance (H,V) | +-------------------------+---------------+------------------------------+ | UIActivityIndicatorView | 750,750 | 750,750 | | UIButton | 250,250 | 750,750 | | UIDatePicker | 250,250 | 750,750 | | UIImageView | 251,251 | 750,750 | | UILabel | 251,251 | 750,750 | | UIPageControl | 250,250 | 750,750 | | UIPickerView | 250,250 | 750,750 | | UIProgressView | 250,750 | 750,750 | | UIScrollView | 250,250 | 750,750 | | UISearchBar | 250,250 | 750,750 | | UISegmentedControl | 250,250 | 750,750 | | UISlider | 250,250 | 750,750 | | UIStepper | 750,750 | 750,750 | | UISwitch | 750,750 | 750,750 | | UITabBar | 250,250 | 750,750 | | UITextField | 250,250 | 750,750 | | UITextView | 250,250 | 750,750 | | UIToolbar | 250,250 | 750,750 | | UIView | 250,250 | 750,750 | +-------------------------+---------------+------------------------------+ 

发现:

  • 所有对象都具有750,750作为内容压缩阻力优先级
  • 大多数人拥有250,250 内容拥抱优先权
  • UIImageViewUILabel都有251,251作为内容拥抱优先级
  • UIActivityIndicatorViewUIStepperUISwitch拥有750,750 内容拥抱优先级
  • UIProgressView拥有250,750 内容拥抱优先权

程序化默认优先级值:

以编程方式创build对象(例如UIButton() )时,您将获得以下优先级。

 +-------------------------+---------------+------------------------------+ | Object | Hugging (H,V) | Compression Resistance (H,V) | +-------------------------+---------------+------------------------------+ | UIActivityIndicatorView | 750,750 | 750,750 | | UIButton | 250,250 | 750,750 | | UIDatePicker | 750,750 | 750,750 | | UIImageView | 250,250 | 750,750 | | UILabel | 250,250 | 750,750 | | UIPageControl | 750,750 | 750,750 | | UIPickerView | 750,750 | 750,750 | | UIProgressView | 250,750 | 750,750 | | UIScrollView | 250,250 | 750,750 | | UISearchBar | 250,750 | 750,750 | | UISegmentedControl | 250,750 | 750,750 | | UISlider | 250,750 | 750,750 | | UIStepper | 750,750 | 750,750 | | UISwitch | 750,750 | 750,750 | | UITabBar | 250,750 | 750,750 | | UITextField | 250,250 | 750,750 | | UITextView | 250,250 | 750,750 | | UIToolbar | 250,750 | 750,750 | | UIView | 250,250 | 750,750 | +-------------------------+---------------+------------------------------+ 

发现:

  • 在IB实例化对象和以下对象的编程实例化对象之间存在内容拥抱优先级差异:UIDatePicker,UIImageView,UILabel,UIPageControl,UIPickerView,UISearchBar,UISegmentedControl,UISlider,UITabBar和UIToolbar。

程序常量

 +----------------------------------+-------+ | Constant | Value | +----------------------------------+-------+ | UILayoutPriorityRequired | 1000 | | UILayoutPriorityDefaultHigh | 750 | | UILayoutPriorityDefaultLow | 250 | | UILayoutPriorityFittingSizeLevel | 50 | +----------------------------------+-------+ 

当你添加一个约束(通过Interface Builder或者通过编程)时,它的默认优先级是1000