Search This Blog

Thursday, October 5, 2017

iOS. Difference between View Frame and Bounds


What’s the difference between the frame and the bounds?

UIView frame is the rectangle described by origin point (x and y) and size (width and height). Location of frame is relative to the superview it is contained within. For example, origin point of frame rectangle is (37.5, 233.5).

UIView bounds is the rectangle described by origin point (x and y) and size (width and height).  Location of bounds is relative to its own coordinate system. For example, origin point of bounds rectangle is (0, 0).

View inside ViewController's root view



Source Code
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var customView: UIView!
    
    @IBOutlet weak var frameRectLabel: UILabel!
    
    @IBOutlet weak var frameOriginLabel: UILabel!
    
    @IBOutlet weak var boundsRectLabel: UILabel!
    
    @IBOutlet weak var boundsOriginLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(self.customView.frame)
        print(self.customView.bounds)
        print(self.customView.frame.origin)
        print(self.customView.bounds.origin)
        
        self.frameRectLabel.text = "Frame Rect: \(self.customView.frame)"
        self.frameOriginLabel.text = "Frame Origin: \(self.customView.frame.origin)"
        self.boundsRectLabel.text = "Bounds Rect: \(self.customView.bounds)"
        self.boundsOriginLabel.text = "Bounds Origin: \(self.customView.bounds.origin)"
        
    }

}

GitHub LInk

4 comments:

  1. There are many articles circulating on internet that exaggerate about Custom Website. But your article is an exception that made me understand it without any difficulty.

    ReplyDelete
  2. Thank you very much for drawing my notice to this. Your blog is jam-packed with helpful information. Unless I read it, I'd have no idea. I'll be back for more great content. Wishing you the best of luck and prosperity. Best Custom Websites

    ReplyDelete
  3. Thank you very much for sharing this informational blog.I could not find such kind of information in any other site.I'll come back for more fantastic material.
    Best wishes, and good luck.
    Custom Website

    ReplyDelete
  4. your article is unbelievable with accurate information. I see a lot of research after that and that is more than I expectedCustom Build Website

    ReplyDelete