iOS Dev: Difference between frame and bound, the easiest explanation you can find nowhere

Every View has frame and bounds

View Properties

What properties do views must have?

Frame

Frame

Bounds

Bounds

Frame vs Bounds

frame vs bounds

Code Example to Help You Understand

let imageView = UIImageView(frame: CGRect(x: 40, y: 40, width: 380, height: 240))imageView.image = UIImage(named: "man")view.addSubview(imageView) // view is parent view of UIImageViewprint(imageView.frame) // frame relative to its parent `view`  => 40, 40, 380, 240print(imageView.bounds) // bounds coordinate system of its own => 0, 0, 380, 240

Let’s add subview to the created ImageView

let imageView = UIImageView(frame: CGRect(x: 40, y: 40, width: 380, height: 240))imageView.image = UIImage(named: "man")view.addSubview(imageView) // view is parent view of UIImageViewprint(imageView.frame) // frame relative to its parent `view`  => 40, 40, 380, 240print(imageView.bounds) // bounds coordinate system of its own => 0, 0, 380, 240let label: UILabel = UILabel(frame: CGRect(x: 20, y: 20, width: 240, height: 40))imageView.addSubview(label) // imageView is parent view of UILabelprint(label.frame) // frame relative to its parent `imageView` => 20, 20, 240, 240print(label.bounds) // bounds coordinate system of its own     =>  0, 0, 240, 40

Conclusion

  • Every view has frame and bounds.
  • Frame is defined by the coordinate system of Parent View.
  • Bounds is view’s own coordinate system.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!