ViewController LifeCycle
we will focused on the differences between viewDidLoad
, viewDidAppear
, and viewDidLayoutSubviews
.
The definition given by Apple on viewDidLoad mentioned that it is called after the controller’s view is loaded into memory. To put it in a simple term, it is the first method that will load.
for instance:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blue //so the first thing will be
}
viewDidAppear: Apple defines this as ‘notifies the view controller that its view was added to a view hierarchy. In another word, it basically means that this is being called when the screen is shown to the user.
override func viewDidAppear() {
super.viewWillAppear(animated) //show the animate of view appears
}
or in a more complicated example:
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
imageView.center.x = self.view.frame.width + 300
let center = slef.view.frame.width / 2
UIView.animate(withDuration: 0.5) {
slef.imageView.center.x = center
self.image.image = UIImage(named: "image")
}
The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.
then, what is viewDidLayoutSubview?
viewDidLayoutSubviews is called every time the view is updated, rotated or changed or it’s bounds change. The keyword here is bounds change.
which means, it define the change of this view when we rotate our phone.
and keep in mind:
viewDidLayoutSubviews will always override viewDidLoad.
for example:
1 This will print out the initial position of the button
2 This will replace the initial position with a new position
本文地址:https://blog.csdn.net/weixin_44337445/article/details/107147796
推荐阅读
-
生命周期感知 Lifecycle
-
Jetpack之Lifecycle的基本使用
-
笔记:载入viewcontroller的几种方式
-
maven项目-修复Plugin execution not covered by lifecycle configuration: org.codehaus.
-
iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】
-
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.8:add-test-source (execution: add-functional-so
-
Android Jetpack架构组件之Lifecycle
-
iOS开发中ViewController的页面跳转和弹出模态
-
AppDelegate调用 ViewController的方法,viewcontrol调APPdelegate里的方法
-
Tomcat8源码分析系列-启动分析(一) Lifecycle