Bright
Bright

Reputation: 5741

View controller origin changes every time it's presented

This is what my view controller should be:

enter image description here

This is what it is sometimes:

enter image description here

I want to display a view controller in the circle, however, almost every time the view controller in the circle (ResultViewController) is presented, it's place is different, though its properties doesn't change at all. Here's my code:

func openCircle(withCenter center: CGPoint, dataSource: ([Items], Int, String)){
    self.addCircle(withCenter: center, dataSource: dataSource)
}

func addCircle(withCenter circleCenter: CGPoint, dataSource: ([Items], Int, String)) {

    let longerSide = fmax(view.frame.size.height, view.frame.size.width)
    let shorterSide = fmin(view.frame.size.height, view.frame.size.width)

    let circleRadius = longerSide / 2
    var resultViewOrigin = CGPoint()
    var resultViewSize = CGSize()

    if UIDevice.current.userInterfaceIdiom == .pad {

        let rectWidth = shorterSide / 2
        let rectHeight = sqrt(abs(circleRadius * circleRadius - rectWidth * rectWidth)) + view.frame.size.height - circleCenter.y
        resultViewSize = CGSize(width: rectWidth, height: rectHeight)
        resultViewOrigin = CGPoint(x: (view.frame.size.width - rectWidth) / 2, y: view.frame.size.height - rectHeight)

    } else {
        resultViewOrigin = CGPoint(x: 0.0, y: 0.0)
        resultViewSize = CGSize(width: view.frame.size.width, height: view.frame.size.height)
    }

    let resultViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ResultVC") as! ResultViewController
    resultViewController.transitioningDelegate = self
    resultViewController.modalPresentationStyle = .custom
    resultViewController.dataSource = dataSource
    resultViewController.view.frame = CGRect(origin: resultViewOrigin, size: resultViewSize)

    transition.circle = UIView()
    transition.startingPoint = circleCenter
    transition.radius = circleRadius
    transition.circle.frame = circleFrame(radius: transition.radius, center: transition.startingPoint)

    present(resultViewController, animated: true)
}

It works well on the iPhone, not on the iPad, what's the problem?

Upvotes: 5

Views: 382

Answers (2)

Bright
Bright

Reputation: 5741

I found the problem, it's actually a missing constraint on Regular-Regular size class caused this problem, I fixed it by adding a spacing to bottom layout guide to the part that used to get misplaced.

Thanks to everybody for your idea.

Upvotes: 3

clemens
clemens

Reputation: 17721

You can use a container view instead of presenting the view controller. You can create them programmatically or in interface builder (see Apple docs).

Upvotes: 1

Related Questions