Reputation: 135
My thoughts are to put a function in the viewController, but when I call it, I just get 0 for both x and y instead of the window position, obviously I am messing up somewhere any ideas?
class ViewController: NSViewController {
func getCoordinate() {
let x = self.view.frame.origin.x
let y = self.view.frame.origin.y
print (x)
print (y)
}
}
Upvotes: 1
Views: 964
Reputation: 271175
You need to get the view.window.frame
, rather than view.frame
.
if let window = view.window {
print(window.frame.origin)
} else {
print("The view is not in a window yet!")
}
Remember to do this only after view
is added to the window, such as in viewDidAppear
.
The view
's frame is relative to view
's superview's origin, not the screen's origin. and it just so happens that in this case, view
is positioned exactly at the origin of its superview (whatever view that is), hence (0, 0).
Upvotes: 4