Explain the view hierarchy in UiKit.

Beginner

Answer

The view hierarchy is a tree-like structure where views contain other views as subviews. Each view has a superview (parent) and can have multiple subviews (children). The root view is typically managed by a view controller.

Key characteristics:

  • Child views are drawn on top of their parent views
  • Coordinate systems are relative to the superview
  • Events bubble up through the hierarchy
  • Views inherit certain properties from their superviews
let parentView = UIView()
let childView1 = UIView()
let childView2 = UIView()

parentView.addSubview(childView1)
parentView.addSubview(childView2)
// childView2 will be drawn on top of childView1