iOS ViewController生命周期

2020-01-13 14:52:23 浏览数 (1)

iOS 显示是 约束 -> 布局 -> 显示的过程,有时候你操作改变frame没有反应是因为还没有确定frame

TestCode


代码语言:javascript复制
//
//  ViewController.swift
//  TestXib
//
//  Created by zhangyu on 2019/11/13.
//  Copyright © 2019 zy. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    override func loadView() {
        super.loadView()
        print("loadView")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("viewDidLoad")
    }
    
    override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
       print("viewWillAppear")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("viewDidAppear")
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print("viewWillLayoutSubviews")
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print("viewDidLayoutSubviews")
    }
    
    override func updateViewConstraints() {
        super.updateViewConstraints()
        print("updateViewConstraints")
    }

}

打印结果


代码语言:javascript复制
loadView
viewDidLoad
viewWillAppear
updateViewConstraints
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear

分析


直接修改Frame

viewDidAppear 才能确定布局,需要在这个方法直接修改frame

如果不想在显示之后修改,需要修改约束,然后Layout,显示。

0 人点赞