我正在尝试调试自动布局问题,并且知道内容拥抱和内容压缩阻力优先级的默认值将有所帮助.
这些是什么?它们是否特定于特定组件?我可以使用常量来引用它们吗?
关键字:UILayoutPriority
解决方法
这些值是
Xcode 7.3的最新版本.
Interface Builder默认优先级值:
将组件拖动到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,750 | | UIImageView | 251,251 | 750,750 | | UILabel | 251,750 | | UIPageControl | 250,750 | | UIPickerView | 250,750 | | UIProgressView | 250,750 | | UIScrollView | 250,750 | | UISearchBar | 250,750 | | UISegmentedControl | 250,750 | | UiSlider | 250,750 | | UIStepper | 750,750 | | UISwitch | 750,750 | | UITabBar | 250,750 | | UITextField | 250,750 | | UITextView | 250,750 | | UIToolbar | 250,750 | | UIView | 250,750 | +-------------------------+---------------+------------------------------+
发现:
>所有对象的内容压缩阻力优先级均为750,750.
>大多数人拥有250,250作为他们的内容拥抱优先权.
> UIImageView和UILabel都有251,251作为其内容拥抱优先级.
> UIActivityIndicatorView,UIStepper和UISwitch的内容拥抱优先级为750,750.
> UIProgressView的内容拥抱优先级为250,750.
程序化默认优先级值:
以编程方式创建对象(例如UIButton())时,您将获得以下优先级.
+-------------------------+---------------+------------------------------+ | Object | Hugging (H,750 | | UIDatePicker | 750,750 | | UIImageView | 250,750 | | UILabel | 250,750 | | UIPageControl | 750,750 | | UIPickerView | 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.