IOS Auto Layout详解一

文章概要


1.概要

最近这段时间有点忙没过多的时间继续写技术博客,先写个大纲过完这阵子再详细补齐。
2.视图大小自适应(自动约束下)-Intrinsic Content Size
3.Intrinsic Content Size 与 sizetofit

-(CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority

-(CGSize)intrinsicContentSize

之间的关系。

视图内容自适应实现的一种方式:

-(void)layoutSubviews{
    [super layoutSubviews];
    if(!CGSizeEqualToSize(self.bounds.size, self.intrinsicContentSize)){
        [self invalidateIntrinsicContentSize];//使系统调用intrinsicContentSize方法并且重新布局
    }
}

-(CGSize)intrinsicContentSize{
    return self.contentSize;
}

自动约束系统

二、约束优先级


约束优先级的数值范围是1~1000,数值越高优先级越高。
官方原文:

IOS Auto Layout详解一_第1张图片

一种情况是我们经常添加的各种约束,默认的优先级是1000,也就是最高级别也就是下面列举的枚举值 UILayoutPriorityRequired.
下面是我们常用的约束的几个优先级:

typedef float UILayoutPriority NS_TYPED_EXTENSIBLE_ENUM;
static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = 1000; // A required constraint.  Do not exceed this.
static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = 750; // This is the priority level with which a button resists compressing its content.
static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = 250; // This is the priority level at which a button hugs its contents horizontally.
static const UILayoutPriority UILayoutPriorityFittingSizeLevel NS_AVAILABLE_IOS(6_0) = 50; // When you send -[UIView systemLayoutSizeFittingSize:], the size fitting most closely to the target size (the argument) is computed.  UILayoutPriorityFittingSizeLevel is the priority level with which the view wants to conform to the target size in that computation.  It's quite low.  It is generally not appropriate to make a constraint at exactly this priority.  You want to be higher or lower.

优先级如下:
UILayoutPriorityRequired > UILayoutPriorityDefaultHigh > UILayoutPriorityDefaultLow > UILayoutPriorityFittingSizeLevel

IOS Auto Layout详解一_第2张图片

二、Intrinsic Content Size


3.1 Intrinsic Content Size Versus Fitting Size

官方文件地址:Anatomy of a Constraint

参考文献:
Auto Layout Guide (官方文档)

iOS 9: UIStackView入门

iOS开发-LayoutGuide的前世今生(从top/bottom LayoutGuide到Safe Area)

是时候了解一下UILayoutGuide了

你可能感兴趣的:(IOS Auto Layout详解一)