Saturday, January 27, 2018

View controller animating in subviews on init when using auto layout?

Leave a Comment

I have a programmatically made view controller with subviews that are positioned using constraints. When I push this view controller into view using a navigation controller with the animation disabled...

let viewController = InventoryViewController() navigationController?.pushViewController(viewController, animated: false) 

...the view controller does not simply appear on screen, its subviews (the ones with constraints) expand outward and into view. Subviews that are not given constraints simply appear on screen as expected. So obviously, auto layout is animating itself into view. How do I disable this animation and just have the subviews appear on screen?

class InventoryViewController: UIViewController {      override func loadView() {          view = UIView()         view.frame = UIScreen.main.bounds         view.backgroundColor = UIColor.white          addButton()      }      func addButton() {          let button = UIButton()         button.backgroundColor = UIColor.black         button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)         button.translatesAutoresizingMaskIntoConstraints = false         view.addSubview(button)         button.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true         button.topAnchor.constraint(equalTo: view.topAnchor, constant: 36).isActive = true         button.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -32).isActive = true         button.heightAnchor.constraint(equalToConstant: 48).isActive = true                }      @objc func buttonAction() {         //        }  } 

5 Answers

Answers 1

Pushing the view controller is what causes its view to be loaded. You could try to force this to load and layout before the push:

let viewController = InventoryViewController() viewController.loadViewIfNeeded() viewController.view.layoutIfNeeded() navigationController?.pushViewController(viewController, animated: false) 

Answers 2

loadView() is not a good place to configure your subviews, it should be used only to put the views in your view hierarchy, probably that is why you view is animating.

Try to set your subviews on viewDidLoad() method.

Please look the discussion section in the official documentation: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview

Answers 3

Call your method from viewDidLayoutSubviews after super.viewDidLayoutSubviews. And animation will not be seen. As the reason behind this is using Autolayout, frame of your views are set when the autolayout engine starts its calculation. This method is called when the autolayout engine has finished to calculate your views' frames

Answers 4

use UIView.setAnimationsEnabled(false)

Answers 5

You use layoutIfNeeded() on the view they are added to, to force layout/redraw immediately.

Do this in viewDidLoad or try right after you have set the constraints.

or try:

layoutSubviews() setNeedsLayout() layoutSubviews() 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment