Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to Use Bound and When to use Frame

I have found out that

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

But my doubt is in which scenario I will have the bounds to be used . Cant we use frames in

all the cases ? Is there a need to use the bound ?

like image 289
Raj Avatar asked Dec 20 '22 14:12

Raj


1 Answers

From the View Programming Guide:

  • The frame property contains the frame rectangle, which specifies the size and location of the view in its superview’s coordinate system.
  • The bounds property contains the bounds rectangle, which specifies the size of the view (and its content origin) in the view’s own local coordinate system.

Frame:

You use the center and frame properties primarily for manipulating the geometry of the current view. For example, you use these properties when building your view hierarchy or changing the position or size of a view at runtime. If you are changing only the position of the view (and not its size), the center property is the preferred way to do so. The value in the center property is always valid, even if scaling or rotation factors have been added to the view’s transform. The same is not true for the value in the frame property, which is considered invalid if the view’s transform is not equal to the identity transform.

Bounds:

You use the bounds property primarily during drawing. The bounds rectangle is expressed in the view’s own local coordinate system. The default origin of this rectangle is (0, 0) and its size matches the size of the frame rectangle. Anything you draw inside this rectangle is part of the view’s visible content. If you change the origin of the bounds rectangle, anything you draw inside the new rectangle becomes part of the view’s visible content.

So you do the following things with the frame:

  • resizing view
  • moving / reposition view

Situation when you should use bounds:

  • when you do drawing inside the view with bounds e.g. in the drawRect: method of UIView.
  • adding subViews to a parent views bounds
like image 199
Pfitz Avatar answered Feb 09 '23 01:02

Pfitz