They are both CGRects
and my program behaves the same when I switch one for the other.
Frame refers to the coordinate system of the view's container (parent view). Bounds refer to the own coordinate system of the view.
TLDR: Bounds refers to the views own coordinate system while Frame refers to the views parent coordinate system.
Short description. frame = a view's location and size using the parent view's coordinate system ( important for placing the view in the parent) bounds = a view's location and size using its own coordinate system (important for placing the view's content or subviews within itself)
Frame A view's frame ( CGRect ) is the position of its rectangle in the superview 's coordinate system. By default it starts at the top left. Bounds A view's bounds ( CGRect ) expresses a view rectangle in its own coordinate system.
See UIView for documentation.
The frame property specifies the origin and size of a view in superview coordinates. The origin of the coordinate system for all views is in the upper-left corner.
The bounds property specifies the origin in the view’s coordinates and its size (the view’s content may be larger than the bounds size).
Frame and bounds are similar but frame is in reference to another object (the superview) while bounds references itself.
This question gives lots of great info. You should definitely read it.
One thing I will point out specifically from the other answer is your program will behave the same sometimes. For example, until you rotate the orientation. From the answer by tristan
Running this code:
- (void)viewDidLoad {
[super viewDidLoad];
UIWindow *w = [[UIApplication sharedApplication] keyWindow];
UIView *v = [w.subviews objectAtIndex:0];
NSLog(@"%@", NSStringFromCGRect(v.frame));
NSLog(@"%@", NSStringFromCGRect(v.bounds));
}
The output of this code is:
case device orientation is Portrait
{{0, 0}, {768, 1024}} <- frame
{{0, 0}, {768, 1024}} <- bounds
case device orientation is Landscape
{{0, 0}, {768, 1024}} <- frame
{{0, 0}, {1024, 768}} <- bounds
So yes your program will usually behave the same but not in all cases.
please go through this link.Hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With