カテゴリー
iOS Swift

View LifecycleのどのタイミングでboundsとsafeAreaInsetsの情報を得るか

override func viewDidLoad() {
  super.viewDidLoad()
  print("printing view.bounds in viewDidLoad: \(view.bounds)")
  print("printing view.safeAreaInsets in viewDidLoad: \(view.safeAreaInsets)")
}

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  print("printing view.bounds in viewWillAppear: \(view.bounds)")
  print("printing view.safeAreaInsets in viewWillAppear: \(view.safeAreaInsets)")
}

override func viewWillLayoutSubviews() {
  super.viewWillLayoutSubviews()
  print("printing view.bounds in viewWillLayoutSubviews: \(view.bounds)")
  print("printing view.safeAreaInsets in viewWillLayoutSubviews: \(view.safeAreaInsets)")
}
	
override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  print("printing view.bounds in viewWillLayoutSubviews: \(view.bounds)")
  print("printing view.safeAreaInsets in viewWillLayoutSubviews: \(view.safeAreaInsets)")
}

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  print("printing view.bounds in viewDidAppear: \(view.bounds)")
  print("printing view.safeAreaInsets in viewDidAppear: \(view.safeAreaInsets)")
}

結果

printing view.bounds in viewDidLoad: (0.0, 0.0, 414.0, 896.0)

printing view.safeAreaInsets in viewDidLoad: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

printing view.bounds in viewWillAppear: (0.0, 0.0, 414.0, 896.0)

printing view.safeAreaInsets in viewWillAppear: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

printing view.bounds in viewWillLayoutSubviews: (0.0, 0.0, 414.0, 896.0)

printing view.safeAreaInsets in viewWillLayoutSubviews: UIEdgeInsets(top: 48.0, left: 0.0, bottom: 34.0, right: 0.0)

printing view.bounds in viewWillLayoutSubviews: (0.0, 0.0, 414.0, 896.0)

printing view.safeAreaInsets in viewWillLayoutSubviews: UIEdgeInsets(top: 48.0, left: 0.0, bottom: 34.0, right: 0.0)

printing view.bounds in viewDidAppear: (0.0, 0.0, 414.0, 896.0)

printing view.safeAreaInsets in viewDidAppear: UIEdgeInsets(top: 48.0, left: 0.0, bottom: 34.0, right: 0.0)