0
votes

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
Try CGPointMake(CGRectGetMidX(self.yourCustomView.bounds), CGRectGetMidY(self.yourCustomView.bounds)); - Avijit Nagare
in what method do you try to get this value ? - nsinvocation
I have center / screenCenter assigned in my GraphView : UIView class. I try drawing / get ouput from overriding drawRect - John
I am trying Avijit's suggestion to no avail, but self can't seem to link to GraphView directly... tried self.(GraphView) but can't seem to get to bounds. - John
when are you reading those value out? the final on-screen frame is defined when the view pushed into the view-hierarchy, therefore you will not get the final frame before that. try to read the value of the frame in –viewWillAppear: method, maybe, instead of the –viewDidLoad:. - holex

2 Answers

0
votes

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
votes

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
    }