Sobes.tech
Junior — Middle

How is graphical information displayed in a UIView component?

sobes.tech AI

Answer from AI

The display of graphical information in a UIView is implemented by overriding the draw(_ rect: CGRect) method.

In this method, you can use Core Graphics to draw primitives, text, images, etc. The system calls draw(_:) when a redraw is needed.

An example of a simple custom UIView with a circle drawing:

class CircleView: UIView {
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        context.setFillColor(UIColor.red.cgColor)
        context.fillEllipse(in: rect)
    }
}

Here, a red circle is created that fills the entire rectangle of the view.

You can also use layers (CALayer) and other mechanisms to display graphics, but draw(_:) is the basic way of custom drawing in UIView.