Cocoa: What's the difference between the frame and the bounds?

后端 未结 11 2242
温柔的废话
温柔的废话 2020-11-22 08:00

UIView and its subclasses all have the properties frame and bounds. What\'s the difference?

相关标签:
11条回答
  • 2020-11-22 08:18

    Frame its relative to its SuperView whereas Bounds relative to its NSView.

    Example:X=40,Y=60.Also contains 3 Views.This Diagram shows you clear idea.

    FRAME

    BOUNDS

    0 讨论(0)
  • 2020-11-22 08:19

    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.

    0 讨论(0)
  • 2020-11-22 08:22

    Bounds - x:0, y:0, width: 20, height: 40 is a static
    Frame - x:60, y:20, width: 45, height: 45 is a dynamic based on inner bounds.

    One more illustration to show a difference between frame and bounds. At this example:

    • View B is a subview of View A
    • View B was moved to x:60, y: 20
    • View B was rotated 45 degrees

    0 讨论(0)
  • 2020-11-22 08:28

    try to run the code below

    - (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}}
    {{0, 0}, {768, 1024}}
    

    case device orientation is Landscape

    {{0, 0}, {768, 1024}}
    {{0, 0}, {1024, 768}}
    

    obviously, you can see the difference between frame and bounds

    0 讨论(0)
  • 2020-11-22 08:34

    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)
    
    1. Page 0: When the ScrollView is at 0 Page the Bounds will be (x:0,y:0, width : 320, height : 200)

    2. 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.

    3. Page 2: Scroll and move to Page 2 Bounds : (x:640,y:0, width : 320, height : 200) Again take a look at frame of imageView2

    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.

    0 讨论(0)
提交回复
热议问题