UIView and its subclasses all have the properties frame and bounds. What's the difference?
UIView and its subclasses all have the properties frame and bounds. What's the difference?
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, imagine a view that has a size of 100x100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view's bounds and frame:
// This method is in the view controller of the superview
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
NSLog(@"bounds.size.width: %f", label.bounds.size.width);
NSLog(@"bounds.size.height: %f", label.bounds.size.height);
NSLog(@"frame.origin.x: %f", label.frame.origin.x);
NSLog(@"frame.origin.y: %f", label.frame.origin.y);
NSLog(@"frame.size.width: %f", label.frame.size.width);
NSLog(@"frame.size.height: %f", label.frame.size.height);
}
And the output of this code is:
bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100
frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100
So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).
There is also a great presentation that covers UIViews. See slides 1-20 which not only explain the difference between frames and bounds but also show visual examples.
iOS Frame vs Bounds
frame and bounds has CGRect type and these properties you can find in UIView and CALayer[About]
Frame has a direct relation with bounds, position, and transform:
bounds.Bounds:
One more illustration to show a difference between frame and bounds. At this example:
View B is a subview of View AView B was moved to x:72, y: 22View B was rotated 45 degreeslet viewB = UIView(frame: CGRect(origin: CGPoint(x: 72, y: 22), size: CGSize(width: 20, height: 40)))
viewB.backgroundColor = .cyan
let radian = 45 * CGFloat.pi / 180
viewB.transform = CGAffineTransform(rotationAngle: radian)
viewA.addSubview(viewB)
All answers above are correct and this is my take on this:
To differentiate between frame and bounds CONCEPTS developer should read:
- relative to the superview (one parent view) it is contained within = FRAME
- relative to its own coordinate system, determines its subview location = BOUNDS
"bounds" is confusing because it gives the impression that the coordinates are the position of the view for which it is set. But these are in relations and adjusted according to the frame constants.
Let me add my 5 cents.
Frame is used by the view's parent view to place it inside the parent view.
Bounds is used by the view itself to place it's own content (like a scroll view does while scrolling). See also clipsToBounds. Bounds also can be used to zoom in/out content of the view.
Analogy:
Frame ~ TV screen
Bounds ~ Camera (zoom, move, rotate)
The answers above have very well explained the difference between Bounds and Frames.
Bounds : A view Size and Location as per its own coordinate system.
Frame : A view size and Location relative to its SuperView.
Then there is confusion that in case of Bounds the X,Y will always be "0". This is not true. This can be understood in UIScrollView and UICollectionView as well.
When bounds' x, y are not 0.
Let's assume we have a UIScrollView. We have implemented pagination. The UIScrollView has 3 pages and its ContentSize's width is three times Screen Width (assume ScreenWidth is 320). The height is constant (assume 200).
scrollView.contentSize = CGSize(x:320*3, y : 200)
Add three UIImageViews as subViews and keep a close look at the x value of frame
let imageView0 = UIImageView.init(frame: CGRect(x:0, y: 0 , width : scrollView.frame.size.width, height : scrollView.frame.size.height))
let imageView1 : UIImageView.init( frame: CGRect(x:320, y: 0 , width : scrollView.frame.size.width, height : scrollView.frame.size.height))
let imageView2 : UIImageView.init(frame: CGRect(x:640, y: 0 , width : scrollView.frame.size.width, height : scrollView.frame.size.height))
scrollView.addSubview(imageView0)
scrollView.addSubview(imageView0)
scrollView.addSubview(imageView0)
Page 0: When the ScrollView is at 0 Page the Bounds will be (x:0,y:0, width : 320, height : 200)
Page 1:
Scroll and move to Page 1.
Now the bounds will be (x:320,y:0, width : 320, height : 200)
Remember we said with respect to its own coordinate System. So now the "Visible Part" of our ScrollView has its "x" at 320. Look at the frame of imageView1.
Same for the case of UICollectionView. The easiest way to look at collectionView is to scroll it and print/log its bounds and you will get the idea.
Frame vs bounds
Here is a great video about it (of Allen Sean):
Swift - Bounds vs. Frame - iOS Interview Question
The Frame is the position in relative to the superview coordinates:

and the Bounds is the position relative to it's own coordinate system.

And... it gets more tricky if the view is rotated. (watch the video!)