2

I'm having some difficulties understanding the difference, and when to use what.

I know the textbook definitions. I have also searched a lot regarding this very topic. Some answers here on SO were helpful to some extent, but I feel I still don't understand this properly.

Let's say I have a aCustomView.m, and when I place UI elements within that view, I use bounds, which makes sense because it's in it's own coordinate system, but, when I initialize the view using initWithFrame: in my view-controller, should I use self.view.frame or self.view.bounds? Both would work, but with different results.

I really want to understand this, so any help would be appreciated.

Nilzone-
  • 2,766
  • 6
  • 35
  • 71

1 Answers1

5

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.

So the difference is only a question of representation.

tiguero
  • 11,477
  • 5
  • 43
  • 61
  • 4
    Just want to add, if you're using self.view.frame, it will often work because that frame happens to have an origin of 0,0. However, as a good rule of thumb. If you're adding a subview and want it to fill up the parent, always set the frame to its parent's bounds. – Logan Apr 22 '14 at 22:00
  • Thanks @Logan for the additional comment. It is indeed a common practice to use the parent's bounds to initialize a subview when you want to fill up the parent. – tiguero Apr 22 '14 at 22:07