UIView: The Building Block of iOS UIs

A UIView is the fundamental building block for iOS UIs. Think of it as a rectangular canvas for drawing content, handling touches, or containing other views. You use it everywhere, from simple buttons to complex layouts.
WHY IT EXISTS: iOS needs a consistent way to manage everything visible on screen. It needs a single abstraction for drawing content, handling user input like taps and swipes, and organizing elements into a hierarchy. UIView is that abstraction.
THE MENTAL MODEL: A UIView is a rectangular area on the screen. It's like a blank canvas that can draw content, or a container that can hold other canvases. Every visual element in an iOS app is part of a tree of views, starting from the main window. This structure is called the view hierarchy.
HOW IT WORKS: Each UIView is responsible for rendering its own content within its rectangular bounds. It can also manage a collection of subviews. The parent view (or "superview") contains the child views ("subviews"). A view's position is defined by its frame or by Auto Layout constraints relative to its superview. Views also handle events. When a user taps the screen, iOS determines which view was tapped and sends it an event message, which it can then respond to.
WHEN TO USE IT: You use UIView constantly in iOS development. Use it directly as a container to group other views, or as a base for custom drawing. More often, you'll use its subclasses like UILabel for text, UIImageView for images, UIButton for buttons, and UITableView for scrollable lists.
WHEN NOT TO USE IT: Do not use a UIView for non-visual tasks. For example, network requests, data processing, or managing application state should not live in your view code. These tasks belong in separate objects (like models or controllers in MVC) to keep the UI responsive. Putting heavy logic in a view is a classic "Massive View Controller" anti-pattern.
ONE CANONICAL EXAMPLE: To create a simple red square, you would instantiate a UIView, set its frame (position and size), and change its backgroundColor. For example: let myView = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50)), then myView.backgroundColor = .red. To make it appear, you would add it as a subview to another view already on screen: self.view.addSubview(myView).
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.