UIKit cheat sheet
The 13 UIKit interview answers in 30 seconds each, plus quick facts. Printable, free.
One page per topic: print it or save it as PDF from your browser's print menu. Practise these questions
The UIViewController lifecycle
A view controller is created (init), loads its view lazily (loadView, then viewDidLoad once), and then goes through appearance callbacks each time it is shown: viewWillAppear, viewIsAppearing, viewDidAppear, and later viewWillDisappear and viewDidDisappear. Do one-time setup in viewDidLoad, and geometry or trait-dependent updates in viewIsAppearing or layout callbacks.
How Auto Layout works
Auto Layout turns constraints into linear equations and solves them with an incremental solver (based on Cassowary), honouring priorities: required constraints must hold, and lower priorities are satisfied as closely as possible. Conflicts happen when required constraints contradict each other. A common cause is forgetting translatesAutoresizingMaskIntoConstraints = false, which adds hidden constraints from the frame.
Content hugging and compression resistance
They tell Auto Layout how a view with an intrinsic size behaves when space doesn't match. Hugging priority resists growing larger than its content; compression resistance resists shrinking smaller. When two labels compete for space, raise the compression resistance of the one that must stay readable, or the hugging of the one that should stay tight.
frame vs bounds
bounds is the view's own coordinate space: its origin (usually zero) and size. frame is the view's position and size in its superview's coordinates. After a transform such as rotation, frame becomes the bounding box of the transformed view and is not meaningful to set; bounds and center still describe the view.
Table and collection view cell reuse
Lists create only enough cells for the screen and reuse them as you scroll: dequeueReusableCell returns a recycled cell that may still show old content. Reset state in prepareForReuse or fully configure every cell. When images load asynchronously, check that the cell still shows the same item before applying the result.
UICollectionView vs UITableView and diffable data sources
UITableView is a single-column list. UICollectionView separates data from layout, so the same data can be a list, a grid or anything custom, and with list configurations it covers table views too. Diffable data sources apply snapshots of identifiers and animate the difference, which removes 'invalid number of items' crashes from manual batch updates.
UICollectionViewCompositionalLayout
A compositional layout builds sections from items, groups and sections, each sized absolutely, fractionally or by estimate. Each section can have its own layout, and orthogonalScrollingBehavior makes a section scroll sideways inside a vertical list. It replaces nested collection views and most custom layout subclasses.
layoutSubviews
layoutSubviews runs during the layout pass, after Auto Layout has computed sizes, so bounds are final there. UIKit calls it when bounds change, subviews are added, or someone calls setNeedsLayout. Override it for geometry-dependent work like corner radii or manual frames, and call super. Don't call it directly; use setNeedsLayout or layoutIfNeeded.
Hit-testing and the responder chain
For a touch, UIKit hit-tests from the window down, choosing the deepest view under the finger that is visible, has user interaction enabled and alpha above 0.01. That view becomes the first responder for the touch. Events and nil-targeted actions then travel up the responder chain: view, superview, view controller, window, application.
Passing data between view controllers
Forward: inject data through the initializer or properties before presenting. Backward: give the child a closure (onSave) or a weak delegate that the parent sets. For data many screens share, use a shared model object that both observe. Avoid reaching into other controllers or broadcasting with NotificationCenter for screen-to-screen data.
Modal presentation styles
Since iOS 13 the default is .automatic, usually a page sheet that leaves the presenting screen visible behind it. Because the presenter stays in the hierarchy, its viewWillDisappear and viewDidDisappear aren't called. .fullScreen removes the presenter's view and does call them. Sheets can be swiped down unless isModalInPresentation is true.
viewDidLoad vs init
init creates the controller and stores its dependencies; the view doesn't exist yet. viewDidLoad runs once after the view and outlets are loaded, so view setup goes there. Touching view in init forces it to load early, which defeats lazy loading and can run setup before dependencies are set.
intrinsicContentSize
intrinsicContentSize is the natural size a view wants from its content, like a label's text size. Auto Layout uses it, together with hugging and compression priorities, so you don't need width and height constraints. In a custom view, override it and call invalidateIntrinsicContentSize() whenever the content changes.
Quick facts
- UIStackView
- Arranges subviews in a row or column using Auto Layout. It has no background of its own before iOS 14 and adds no extra drawing.
- Safe area
safeAreaLayoutGuideandsafeAreaInsetskeep content clear of the notch, Dynamic Island, home indicator and bars.- UINavigationController
- Manages a stack of view controllers with push and pop, a navigation bar and a back gesture.
- UIPageViewController
- Pages between view controllers with scroll or page-curl transitions; supply them through a data source.
- UISplitViewController
- Primary and secondary (plus optional supplementary) columns on iPad and Mac; collapses on iPhone.
- NSAttributedString
- Text with per-range attributes (font, colour, links). In Swift, AttributedString (iOS 15) is the value-type alternative.
- draw(_:)
- Override only for custom Core Graphics drawing; it is CPU-bound, so prefer layers or images for static content.