Search code examples
iosuiviewuiscreen

How to get screen coordinates of view inside of keyboard accessory view?


Basically, i have put accessory view on top of the keyborad, inside that view i have view, for which i need the the position, but relative to whole screen (like i am putting it on the whole screen) and not the superview?

I kinda tried it like this:,

How to get the absolute coordinates of a view

but to no avail.


Solution

  • You can convert a frame (CGRect) or a point (CGPoint) from its superview's coordinate system to another view's coordinate system. All these methods are instance methods of UIView

    For example:

    let convertedRect = superview.convertRect(subview.frame, toView: anotherView)

    In your case, you might try something like this:

    let cRect = keyboardAccessoryView.convertRect(accessorySubview.frame, toView: self.view)

    Note that this will convert the subview (whose frame is in the keyboardAccessory's coordinate space) to the current viewController's view's coordinate system.

    If you want it in terms of the entire screen, try this:

    let coordinateSpace = UIScreen.mainScreen().coordinateSpace
    kbAccessoryView.convertRect(subview.frame, toCoordinateSpace: coordinateSpace)