UIKit Reference

Kip Landergren

(Updated: )

My cheat sheet for UIKit covering terminology, configuration tips for objects like UIViewController, UILabel, and UIButton, and helpful links.

Contents#

Design#

Dimensions#

Devices#

iPhone SE 1334px x 750px

Terminology#

anchor#
1.

A factory class for creating layout constraints from view attributes.

Related:

baseline#
1.

Line at bottom of text.

Related:

Layout Constraint#
1.

An equation that defines how a view attribute should exist, often in relationship to another view attribute.

Related:

Leading Edge#
1.

The first edge encountered when scanning in reading order (locale-dependent; changes with right-to-left or left-to-right languages)

Related:

Trailing Edge#
1.

The last edge encountered when scanning in reading order (locale-dependent; changes with right-to-left or left-to-right languages)

Related:

View Attribute#
1.

Part of the object’s visual representation, typically corresponding to an edge or dimension, that should be used to get the value for the layout constraint.

Related:

UIViewController#

Properties#

Prefer lazy loading when the property represents a simple element of the view controller’s view hierarchy:

private lazy var _closeButton: UIBarButtonItem = {
  UIBarButtonItem(title: "Close", style: .plain,
           target: self, action: #selector(t_closePressed(_:)))
}()

For more complex view configuration, consider refactoring.

Prefer implicitly unwrapped optionals when the property is nil after initialization but known to be with value before use, and if nil upon use would be an error:

private var _modalViewController: MyModalViewController!

Prefer optionals for any publically accessible properties, like delegates:

var editingDelegate: EditingDelegate?

Prefer normal types for non-optional values passed or created during initialization:

private var _name = "Editor"

Subclassing#

Initialization#

Create any required data structures to put view controller into a known good state.

Specifying a convenience initializer, noting the call to self.init:

convenience init() {
  self.init(nibName: nil, bundle: nil)
  title = NSLocalizedString("title", comment: "title")
  // set up any navigationItem properties here, not in loadView()
}

Specifying a custom initializer, noting:

init(arg: String) {
  // set up any internal state
  _arg = arg
  super.init(nibName: nil, bundle: nil)
  title = NSLocalizedString("title", comment: "title")
  // set up any navigationItem properties here, not in loadView()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}

loadView()#

Create and wire together your view hierarchy, ensuring the root view is assigned to .view. Do not call super.

viewDidLoad()#

View hierarchy exists in a valid state. Load any data necessary for display, and add any initial constraints.

Adding a Child View Controller#

addChild(viewController)
view.addSubview(viewController.view)
viewController.didMove(toParent: self)

UINavigationController#

Generally: do not set translatesAutoresizingMaskIntoConstraints to false on views being embedded in a UINavigationController.

When presenting modally from a navigation controller, the presented view controller is not embedded in the navigation controller doing the presenting. Therefore if you wish to have navigability in the modal, embed the view controller in a separate navigation controller before presenting.

UITableViewController#

Generally: a UITableViewController should take up the whole screen. If a view controller needs the functionality of a UITableView, create a custom UIViewController to address.

Styles#

UITableView#

Quick facts:

From the docs:

The table view’s delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell.

UINavigationItem#

Object containing the views and buttons for display in the navigation bar.

Uses:

Notes:

UIStackView#

UIStackView is nonrendering, meaning it provides no user interface of its own. This means settings like backgroundColor have no effect.

UILabel#

A UILabel’s default backgroundColor is transparent.

System-provided .textColor values include:

Font#

label.font = UIFont.preferredFont(forTextStyle: .headline)
label.adjustsFontForContentSizeCategory = true
label.adjustsFontSizeToFitWidth = true
label.allowsDefaultTighteningForTruncation = true
label.baselineAdjustment = .alignCenters

Accessibility#

label.accessibilityIdentifier = "Unique Article Title"

UIButton#

UIButton Creation#

_button = UIButton(type: .custom)

UIButton.ButtonType Values#

custom
system
detailDisclosure
infoLight
infoDark
contactAdd
roundedRect

UIBarButtonItem#

isEnabled enables or disables the state of the button

Anchors#

A full list is within the enum NSLayoutConstraint.Attribute.

X Axis#

leadingAnchor a view’s leading edge. should be preferred especially for RTL and LTR localization.
trailingAnchor a view’s trailing edge. should be preferred especially for RTL and LTR localization.
leftAnchor a view’s left edge
rightAnchor a view’s right edge
centerXAnchor a view’s horizontal center

Y Axis#

topAnchor a view’s top edge
bottomAnchor a views bottom edge
firstBaselineAnchor baseline of first line of a view containing multiple lines of text
lastBaselineAnchor baseline of last line of a view containing multiple lines of text
centerYAnchor

Dimensional#

widthAnchor a view’s width
heightAnchor a view’s height

Resources#

Apple Developer Documentation#

Note

I have found that the built-in Xcode documentation for UIKit is not as up-to-date as what is available on developer.apple.com.

Related Documents#