Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive view controller transition from view controller with status bar hidden

Tags:

ios

swift

Here is my demo project.

I have two view controllers. The main one has the status bar hidden while the second one hasn't.

I created a custom driven transition animation to go from controller one to controller two.

When I'm on the child view controller (the orange one), I start the driven transition by panning from top to bottom. You can see that the status bar is coming back when dragging. And the UIButton "Hello" is moving as well.

I cancel the transition. Then I start it again and you can see the status bar is coming back as well but this time, my button isn't moving, it stays at the same location, as if the status bar was still hidden.

Any idea why it would behave like this once the transition has been cancelled at least once?

(I'm not even talking about the weird thing with the animation that is kind of doubled when cancelling (maybe a bug with the simulator as it doesn't do it on my iphone 6 9.1 and my iphone 5 8.4.)

enter image description here

like image 939
Nico Avatar asked Nov 25 '15 01:11

Nico


1 Answers

Add: import Foundation

Then add an outlet:

class ViewController: UIViewController { @IBOutlet weak var topConstraint: NSLayoutConstraint! ... } Then change the value to 0 when the view disappears and then to 20 when it will appear:

override func viewWillAppear(animated: Bool) {
    topConstraint.constant = 20.0
}

override func viewWillDisappear(animated: Bool) {
    topConstraint.constant = 0.0
}

Full code (make sure to remember to connect the constraint to the outlet):

import UIKit
import Foundation

class ViewController: UIViewController {

    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    let controllerTransition = InteractiveControllerTransition(gestureType: .Pan)
    let controllerTransitionDelegate = ViewController2Transition()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        controllerTransition.delegate = controllerTransitionDelegate
        controllerTransition.edge = .Bottom


    }

    override func viewWillAppear(animated: Bool) {
        topConstraint.constant = 20.0
    }

    override func viewWillDisappear(animated: Bool) {
        topConstraint.constant = 0.0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func unwindToViewController(sender: UIStoryboardSegue) { }

    override func prefersStatusBarHidden() -> Bool {
        return false
    }

    @IBAction func helloButtonAction(sender: UIButton) {
//      let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//      let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
//      
//      vc.transitioningDelegate = controllerTransition
//      controllerTransition.toViewController = vc
//      
//      self.presentViewController(vc, animated: true, completion: nil)

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
//      let nvc = storyBoard.instantiateViewControllerWithIdentifier("NavigationViewController2") as! UINavigationController
//      let vc = nvc.topViewController as! ViewController2

        let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2





//      nvc.transitioningDelegate = controllerTransition
        vc.transitioningDelegate = controllerTransition
        controllerTransition.toViewController = vc

//      self.presentViewController(nvc, animated: true, completion: nil)
        self.presentViewController(vc, animated: true, completion: nil)

    }

}
like image 167
FredLoh Avatar answered Oct 29 '22 17:10

FredLoh