Get position of UIView in respect to its superview's superview Get position of UIView in respect to its superview's superview ios ios

Get position of UIView in respect to its superview's superview


You can use this:

Objective-C

CGRect frame = [firstView convertRect:buttons.frame fromView:secondView];

Swift

let frame = firstView.convert(buttons.frame, from:secondView)

Documentation reference:

https://developer.apple.com/documentation/uikit/uiview/1622498-convert


Although not specific to the button in the hierarchy as asked I found this easier to visualize and understand:

From here: original source

enter image description here

ObjC:

CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:viewController.view];

Swift:

let point = subview1.convert(subview2.frame.origin, to: viewControll.view)


Updated for Swift 3

    if let frame = yourViewName.superview?.convert(yourViewName.frame, to: nil) {        print(frame)    }

enter image description here