Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View controller origin changes every time it's presented

Tags:

ios

swift

uiview

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?

like image 695
Bright Avatar asked Nov 02 '16 10:11

Bright


2 Answers

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.

like image 97
Bright Avatar answered Oct 22 '22 15:10

Bright


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

like image 21
clemens Avatar answered Oct 22 '22 15:10

clemens