Search code examples
iosiphoneswiftsize-classesuiviewanimationtransition

View controller after transition is fitting its contraints to size class instead of device


First of all, I followed this tutorial: https://www.youtube.com/watch?v=B9sH_VxPPo4 and then I used this transition in a real project where I work, but since then I am facing a problem with it. A problem that I can't quite understand why it is happening. Perhaps you could help me out.

What's happening is the following: I'm making a tutorial for my app using this transition. In the view controller where I set the text for the tutorial, I set constraints for top, bottom, leading and trailing to 0. Thus, it should fit the view controller to the edges, right? But it only happens when I have chosen in size class the same device that I am using in the simulator. For instance, I must run an iPhone 7 plus simulator AND set the size classe to iPhone 7 plus in order to work properly. When I run the simulator of iPhone 7 without having changed anything in interface builder, It crashes. I realized that the view controller is not fitting to the edges of the device (simulated or physical), it's fitting to the edges of the size class. I realized that when I comment the lines with ".transitioningDelegate = self and .modalPresentationStyle = .custom" it works as it should (but without the transitioning, of course). I thought it could be a problem with this transition. Can you help me out?

Here goes a snippet of the code I'm using to present the view controller using this transition:

let levelTutorial = CPQuizLevelTutorialViewController()
levelTutorial.transitioningDelegate = self
levelTutorial.modalPresentationStyle = .custom
self.present(levelTutorial, animated: true, completion: nil)

Here goes another part of my code where I implement the UIViewControllerTransitioningDelegate:

extension CPMainQuizViewController: UIViewControllerTransitioningDelegate {

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.transitionMode = .dismiss
        transition.startingPoint = self.view.center
        transition.circleColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha:0.6)

        return transition
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.transitionMode = .present
        transition.startingPoint = self.view.center
        transition.circleColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha:0.6)

        return transition
    }
}

Here is a picture of the interface builder. I put a blue background for facilitating the visualization of the problem

Here's what happens

I don't know if it has something related to the error, but I'm using xib instead of Storyboard. One thing I do know it's related to the bug is that I'm pretty new to iOS development. So, first of all, sorry if I said something stupid. LOL

Thanks in advance, guys!


Solution

  • Well, I found a workaround for this.

    I added this line of code in every view controller I was having this problem:

    self.view.frame.size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    

    After have done it, screens behave perfectly adjusting its edges to the smartphone edges. :D

    I still don't know what caused this error. So, if anyone has any ideas about it, share with us. :D