HeroTransitions / Hero

Elegant transition library for iOS & tvOS
https://HeroTransitions.github.io/Hero/
MIT License
22.06k stars 1.73k forks source link

Hero doesn't work in UIViewControllers created programmatically #35

Closed FilipZawada closed 7 years ago

FilipZawada commented 7 years ago

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❓

💯

lkzhao commented 7 years ago

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

FilipZawada commented 7 years ago

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

lkzhao commented 7 years ago

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

FilipZawada commented 7 years ago

Yes

FilipZawada commented 7 years ago

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

lkzhao commented 7 years ago

Sure. Let me know.

FilipZawada commented 7 years ago

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.

FilipZawada commented 7 years ago

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 {
}
lkzhao commented 7 years ago

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.

leobouilloux commented 7 years ago

@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.

tylerswartz commented 7 years ago

@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)
leobouilloux commented 7 years ago

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.

pradeepmaretha commented 4 years ago

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

VatanaChhorn commented 3 years ago

Is that working when we use the push view method? I tried to present the view controller and it works, however, when it comes to pushing the view controller, the transition animation doesn't present itself.