Hero doesn't work in UIViewControllers created programmatically

Created on 10 Jan 2017  ·  13Comments  ·  Source: HeroTransitions/Hero

I'm trying to use Hero in view controllers created purely in code, without storyboards, with pushViewController and it's not working:

class LiveInjection3ViewController: UIViewController, HeroViewControllerDelegate {
  func heroWillStartTransition() {
    // ...
  }
}

let vc = LiveInjection3ViewController()
vc.isHeroEnabled = true
navigationController.pushViewController(vc, animated: true)

Is it not working because:
a) Hero supports segues only❓
b) Hero supports view controllers created with storyboard only❓
c) I missed sth else❓

💯

Most helpful comment

It should work. Is your navigationController's isHeroEnabled set to true?

All 13 comments

It should work. Is your navigationController's isHeroEnabled set to true?

yup, I even tested this on Injection Example you provide (where navigationController should have isHeroEnabled = true, since it's already working)

So what are you seeing? The views are suddenly appearing without animations?

Yes

I'm sorry, I started preparing code to give you example, and it started showing some animations, hmm, will keep updated

Sure. Let me know.

Ok, so if the view controller doesn't have background color specified, then it's not working as expected. When I added controller.view.backgroundColor = .white it started fading nicely.

This is a case when no modifiers are set (and I expect things to just fade). If you add some modifiers, then some animations are partially visible.

Is it a bug? If you want to check it out yourself, please paste this code to your sample app's LiveInjection2ViewControllers.swift file:

//
//  LiveInjection2ViewController.swift
//  HeroExamples
//
//  Created by YiLun Zhao on 2017-01-04.
//  Copyright © 2017 Luke Zhao. All rights reserved.
//

import UIKit
import Hero

class LiveInjection2ViewController: UIViewController {

    @IBOutlet weak var orangeView: UIView!
    @IBOutlet weak var greenView: UIView!
    @IBOutlet weak var blueView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        delay(3.0) {
            let controller = LiveInjection3ViewController()
//            controller.view.backgroundColor = .white
            controller.isHeroEnabled = true
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }

}

extension LiveInjection2ViewController: HeroViewControllerDelegate {
}

class LiveInjection3ViewController: UIViewController, HeroViewControllerDelegate {
}

Don't think this is a bug. If you don't set the view's background. Then the view you created programmatically doesn't have any content (transparent). Therefore Hero is fading a transparent view on top (an animation which you cannot see). After Hero is done animating, the navigationController will remove the previous view controller from the stack. That is when you see the black background. Controllers created in storyboard will have a background color of white by default.

@FilipZawada Pretty sure it's too late, but I've got the same problem than you, I just did navigationController.isHeroEnabled = true and it works.

@leobouilloux where did you put navigationController.isHeroEnabled = true in your navigation controller?

I'm having trouble transitioning between two view controllers. I can successfully navigate to the new view controller, but none of the buttons are responsive. When I debug the view hiearchy the first view controller is still on top of the new view controller. It appears to be not visible, but still blocking user interactions.

My segue in VC1

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let VC2 = storyBoard.instantiateViewController(withIdentifier: "VC2") as! VC2
VC2.isHeroEnabled = true
VC2.heroModalAnimationType = .zoomSlide(direction: HeroDefaultAnimationType.Direction.left)
self.present(VC2, animated: true)

Hello @tylerswartz,

I have tested on my side and I have no problem:

let vc2 = VC2()
vc2.isHeroEnabled = true
vc2.heroModalAnimationType = .zoomSlide(direction: .left)
present(vc2, animated: true, completion: nil)

Maybe this is a problem related to storyboards.

Does it work when we present the view controller? not with push?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joeljfischer picture joeljfischer  ·  3Comments

aloisdeniel picture aloisdeniel  ·  5Comments

Tibb picture Tibb  ·  3Comments

cristinaITdeveloper picture cristinaITdeveloper  ·  4Comments

dimabiserov picture dimabiserov  ·  3Comments