Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConvertRect doesn't work as expected

I'm trying to convert deep 'Subview' frame to an upper 'UIView'. I'm attaching the view hierarchy here.

Attaching illustration:

enter image description here

I've tried this, but the result are way off screen :

let rect = smallSubview.convert(smallSubview.frame, to: bigSuperview)

I'm trying to convert the small 'Subview' frame, to 'VideoCrop'/bigSuperView coordinate space. Any suggestions? Thank you!

like image 987
Roi Mulia Avatar asked Sep 16 '26 19:09

Roi Mulia


1 Answers

Not sure, but shouldn't you be considering bounds rather than frame of your smallSubView ??

I mean :

let rect = smallSubview.convert(smallSubview.bounds, to: bigSuperview)

EDIT

I could not have answered your comment in answer hence updating my answer :)

The quick view of convert API suggests

func convert(_ rect: CGRect, to view: UIView?) -> CGRect Description
Converts a rectangle from the receiver’s coordinate system to that of another view.

Parameters

rect A rectangle specified in the local coordinate system (bounds) of the receiver.

view The view that is the target of the conversion operation. If view is nil, this method instead converts to window base coordinates. Otherwise, both view and the receiver must belong to the same UIWindow object.

As it suggests you should be considering bounds rather than frame :)

Whats the difference between frame and bounds then ??

Bounds : Specifies the views location and size of view in its own coordinate system. Frame: While this specifies the location and size of view in its superViews coordinate system :)

Hence bounds of any view will have its origin as (0,0) where as frame has its x and y with respect to its superview :) while height and width being same :)

like image 117
Sandeep Bhandari Avatar answered Sep 19 '26 09:09

Sandeep Bhandari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!