美文网首页
AutoLayout

AutoLayout

作者: 李波小丑 | 来源:发表于2017-03-03 23:06 被阅读0次

一般来说layoutSubviews负责布局,比如调整View之间的距离,大小,drawRect负责绘制,比如使用什么颜色。而AutoLayout则是在layout之前增加了一个设定约束的过程,也就是update constraints。

约束方法

/*
 * 当一个自定义view的某个属性发生改变,并且可能影响到constraint时
 * 需要调用此方法去标记constraints需要在未来的某个点更新
 * 系统然后调用updateConstraints
 */
- (void)setNeedsUpdateConstraints{}


/*
 * 系统在需要时,立即出发约束更新,自动更新布局
 */
- (void)updateConstraintsIfNeeded{}

/*
 * 自定义view应该重写此方法,在其中建立constraints
 * 要的实现的最后调用 [super updateConstraints]
 */
- (void)updateConstraints{}

自动布局过程

updateConstraints -> layout -> display

updating constrains

*测量阶段:from subview to super view , 为下一步layout做准备。
可以通过setNeedUpdateConstraints触发此步。constraints的改变也会自动触发此步。
最后会触发- (void)updateConstraints{};所有设置的constraints最终还是走此方法,可以在此方法中最后更改constraints

layout

*布局阶段:from super view to subview ,根据上一步的信息去设置view的center和bounds。
可以通过setNeedsLayout去触发此步,此方法不会立即更新layout
可以通过layoutIfNeeded 立即更新layout
最后会调用layoutSubViews方法。

display

*渲染、展示阶段:from super view to subview
通过调用setNeedsDisplay触发

不使用自动布局

// 
- (void)layoutSubviews{}

// 标记为需要从新布局,异步调用layoutIfNeeded刷新布局,
// 不立即刷新,但layoutSubviews一定会被调用
- (void)setNeedsLayout{}

// 有需要刷新标记时,立即调用layoutSubviews进行布局
// 如果没有标记,不会调用layoutSubviews
- (void)layoutIfNeeded{}

相关文章

  • 9.4 AutoLayout使用

    AutoLayout使用 AutoLayout使用.png

  • iOS - AutoLayout -2 AutoLayout

    # iOS - AutoLayout -2 AutoLayout 上篇文章我们了解了AutoLayout 的布局方...

  • ScrollView 与 Autolayout

    ScrollView 与 Autolayout ScrollView 与 Autolayout

  • # iOS - AutoLayout -1

    iOS - AutoLayout -1 1、AutoLayout 自动布局(AutoLayout)是iOS6引入的...

  • iOS布局

    布局方式 AutoLayout,AutoresizingMask AutoLayout NSLayoutConst...

  • AutoLayout

    AutoLayout autolayout的概念 Autolayout是一种“自动布局”技术,专门用来布局UI界面...

  • 9.6AutoLayout约束基础

    AutoLayout约束基础 AutoLayout约束基础1.png AutoLayout约束基础2.png Au...

  • UI基础4

    自动布局 autoresizing:autolayout:size classes + autolayout:si...

  • AutoLayout

    AutoLayout深入浅出一[前传] AutoLayout深入浅出二[基本使用] AutoLayout深入浅出三...

  • autoLayout 和 autoresizing

    autolayout 和 autoresizing 不能够共存,使用autoLayout必须关闭aoturesizing

网友评论

      本文标题:AutoLayout

      本文链接:https://www.haomeiwen.com/subject/gsfdgttx.html