Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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() {

       //  

    }

}
like image 890
youareawaitress Avatar asked Jan 16 '18 00:01

youareawaitress


People also ask

How do I customize the animated transition between view controllers?

The animated transition between view controllers in iOS 7 is fully customizable. UIViewController now includes a TransitioningDelegate property that provides a custom animator class to the system when a transition occurs. To use a custom transition with PresentViewController:

How do I use a custom animator in UIViewController?

UIViewController now includes a TransitioningDelegate property that provides a custom animator class to the system when a transition occurs. To use a custom transition with PresentViewController: Set the ModalPresentationStyle to UIModalPresentationStyle.Custom on the controller to be presented.

How do I animate the second Controller's View from the bottom?

For example, the following code presents a view controller of type ControllerTwo - a UIViewController subclass: Running the app and tapping the button causes the default animation of the second controller’s view to animate in from the bottom, as shown below:

What is UIViewController animated transition support?

UIKit adds support for customizing the animated transition that occurs when presenting view controllers. This support is included with built-in controllers, as well as any custom controllers that inherit directly from UIViewController.


4 Answers

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.view.layoutIfNeeded()
navigationController?.pushViewController(viewController, animated: false)
like image 83
Thor Frølich Avatar answered Oct 21 '22 09:10

Thor Frølich


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

like image 45
Bruno Guidolim Avatar answered Oct 21 '22 08:10

Bruno Guidolim


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

like image 31
vatsal raval Avatar answered Oct 21 '22 10:10

vatsal raval


use UIView.setAnimationsEnabled(false)

like image 23
Sahabe Alam Avatar answered Oct 21 '22 08:10

Sahabe Alam