getting wrong center of frame for detail view for UISplitView

411 Views Asked by At

learning SWIFT. I can't seem to access bounds from my view... no matter what platform I work in the bounds return the same values (600,600).

I have a graphView : UIView & controller embedded in a navigationController as the Detail of a SplitViewController.

I am trying to get the center of my graphView : UIView on screen (the Detail of the splitViewController), but center keeps returning a point too far to the right (in portrait)/bottom (in landscape).

I tried accessing it multiple ways, but maybe my understanding of it is wrong?

example: var screenCenter: CGPoint = convertPoint( center, fromView: superview)

    println("bounds are  \(bounds)") // view boundaries
    println("frame is at \(frame)")  // frame where the view resides
    println(" center is at       \(center)")
    println(" ScreenCenter is at \(screenCenter)")

(output)

bounds are  (0.0,0.0,600.0,600.0)
frame is at (0.0,0.0,600.0,600.0)
 center is at       (300.0,300.0)
 ScreenCenter is at (300.0,300.0)

-> how do I actually get to the center? Is SplitView geography joint master + detail?

-> it seem the x positioning is correct in portrait (but not the y), and the y is correct in portrait (but not the x)

2

There are 2 best solutions below

0
John On BEST ANSWER

SOLVED! the graphView in Storyboard was missing constraints and using the default 600,600 values. "Reset to Suggested Constraints" in storyboard with the view selected fixed the issue

0
Akos Farkas On

If you do not use storyboard this can help. Create a view where you set up the autoconstraints to false. Constraint the view to the safeAreaLayoutGuide.It will find the centre of your secondary view or detail view.

  var logoView :UIImageView = {
    let iv = UIImageView()
    iv.contentMode = .scaleAspectFit
    iv.backgroundColor = .green
    iv.translatesAutoresizingMaskIntoConstraints = false
    return iv
}()

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(logoView)
    logoView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
    logoView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
    logoView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    logoView.widthAnchor.constraint(equalToConstant: 200).isActive = true
    }