Skip to content

Instantly share code, notes, and snippets.

@johneris
Created August 7, 2017 06:16
Show Gist options
  • Save johneris/9c574196e6456bd11f75dd956fe3bffc to your computer and use it in GitHub Desktop.
Save johneris/9c574196e6456bd11f75dd956fe3bffc to your computer and use it in GitHub Desktop.
iOS UIView Extension - Get the frame of view relative to the desired superview
import UIKit
extension UIView {
func frameRelative(to targetSuperView: UIView) -> CGRect {
var view = self
var rect = view.frame
while let sv = view.superview {
if sv == targetSuperView {
return rect
}
rect = view.convert(rect, to: sv)
view = sv
}
return rect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment