Class

UITab​Bar

A UITab​Bar object is a control for selecting between different subtasks, views, or modes in an app. Normally, you use tab bars in conjunction with a UITab​Bar​Controller object, but you can also use them as standalone controls in your app. Tab bars always appear across the bottom edge of the screen and display the contents of one or more UITab​Bar​Item objects. A tab bar’s appearance can be customized with a background image or tint color to suit the needs of your interface. Tapping an item selects and highlights that item, and you use the selection of the item to enable the corresponding mode for your app.

Overview

You can configure tab bars programmatically or in Interface Builder. A UITab​Bar​Controller object provides its own tab bar object and you must configure the object provided to you. When creating a tab bar programmatically, use the init(frame:​) method or another view initializer method to set its initial configuration. Use the methods of this class to configure the appearance of the tab bar. For tab bars you create yourself, you also use the methods of this class to specify the items displayed by the tab bar.

A tab bar reports selections and user customizations to its delegate object. For tab bars you create yourself, use the delegate to respond to selections or to the addition, removal, or reordering of items in the tab bar. (A UITab​Bar​Controller object acts as the delegate for the tab bar it manages.) For more information on implementing a tab bar delegate, see UITab​Bar​Delegate.

Configuring the Items of a Tab Bar

You can configure tab bar items using Interface Builder or create and configure them programmatically in your code. Tab bars in Interface Builder come preconfigured with some initial items and you can add, remove, or reorder items as needed. How you configure items at design time depends on whether your tab bar is associated with a UITab​Bar​Controller object:

  • Configuring your tab bar in Interface Builder:

    • When a UITab​Bar​Controller object is present, add or remove view controllers to your scene and create relationship segues between the tab bar controller and each new view controller. Creating a relationship segue automatically adds a new item to the tab bar, and deleting an existing relationship segue removes the corresponding tab bar item.

    • When a tab bar controller is not present, drag tab bar items from the library onto your tab bar.

  • Configuring your tab bar programmatically:

    • To configure the tab bar associated with a UITab​Bar​Controller object, configure the view controllers associated with the tab bar controller. The tab bar automatically obtains its items from the tab​Bar​Item property of each view controller associated with the tab bar controller.

    • To configure tab bar items directly, use the set​Items(_:​animated:​) method of the tab bar itself.

A tab bar displays all of its tabs onscreen at once, using the item​Positioning property to determine how to position items in the available space. If you have more items than can fit in the available space, display only a subset of them and let the user select which tabs are displayed. The begin​Customizing​Items(_:​) method displays an interface for selecting which tab bar items to display.

The contents of each item are stored in a UITab​Bar​Item object. Each item contains a title and an image to display in the tab. You can also use tab bar items to add a badge to the corresponding tab. For more information about creating and configuring items, see UITab​Bar​Item.

Responding to Tab Selections

For tab bars with an associated tab bar controller, the tab bar controller automatically manages selections and displays the appropriate view controller. The only time you have to manage selections yourself is when you create the tab bar without a tab bar controller. The tab bar reports selections to the tab​Bar(_:​did​Select:​) method of its delegate object, which you can use to respond to selection changes. For more information about implementing the delegate object, see UITab​Bar​Delegate.

Interface Builder Attributes

Table 1 lists the attributes that you configure for tab bars in Interface Builder.

Table 1

Tab bar attributes

Attribute

Discussion

Background

The background image to display for the bar. If you specify a stretchable image, the image is stretched to fit the available space; otherwise, the image is tiled. When you configure a background image, the tab bar ignores the tint color information. To set this attribute programmatically, use the background​Image property.

Shadow

The custom shadow image for the tab bar. This attribute is ignored if the tab bar does not also have a custom background image. To set this attribute programmatically, use the shadow​Image property.

Selection

The image to use for the selected tab. To set this attribute programmatically, use the selection​Indicator​Image property.

Image Tint

The tint color to apply to the selected item. To set this attribute programmatically, use the tint​Color property.

Style

The basic style to apply to the bar. You can configure a tab bar with a dark or light style and the bar can be opaque or translucent. To set the style programmatically, use the bar​Style and is​Translucent properties.

Bar Tint

The tint color to apply to the bar. To set this attribute programmatically, use the bar​Tint​Color property.

Item Positioning

The positioning scheme to apply to items. Use this attribute to configure how items are spaced across the length of the tab bar. To set this attribute programmatically, use the item​Positioning property.

Internationalization

To internationalize a tab bar, you must provide localized strings for the tab bar item titles.

For more information, see Internationalization and Localization Guide.

Accessibility

Tab bars are accessible by default.

With VoiceOver enabled on an iOS device, when a user touches a tab in a tab bar, VoiceOver reads the title of the tab, its position in the bar, and whether it is selected. For example in the iTunes app on iPad, you might hear “Selected, Audiobooks, four of seven” or “Genius, six of seven.”

For general information about making your interface accessible, see Accessibility Programming Guide for iOS.

Symbols

Configuring Tab Bar Items

var items:​ [UITab​Bar​Item]?

The items displayed by the tab bar.

func set​Items([UITab​Bar​Item]?, animated:​ Bool)

Sets the items on the tab bar, optionally animating any changes into position.

var selected​Item:​ UITab​Bar​Item?

The currently selected item on the tab bar.

Customizing Tab Bar Appearance

var bar​Style:​ UIBar​Style

The tab bar style that specifies its appearance.

var is​Translucent:​ Bool

A Boolean value that indicates whether the tab bar is translucent.

var bar​Tint​Color:​ UIColor?

The tint color to apply to the tab bar background.

var item​Positioning:​ UITab​Bar​Item​Positioning

The positioning scheme for the tab bar items in the tab bar.

var item​Spacing:​ CGFloat

The amount of space (in points) to use between tab bar items.

var item​Width:​ CGFloat

The width (in points) of tab bar items.

var tint​Color:​ UIColor!

The tint color to apply to the tab bar items.

var background​Image:​ UIImage?

The custom background image for the tab bar.

var shadow​Image:​ UIImage?

The shadow image to use for the tab bar.

var selection​Indicator​Image:​ UIImage?

The image to use for the selection indicator.

Setting the Delegate

var delegate:​ UITab​Bar​Delegate?

The tab bar’s delegate object.

Supporting User Customization of Tab Bars

func begin​Customizing​Items([UITab​Bar​Item])

Presents a standard interface that lets the user customize the contents of the tab bar.

func end​Customizing(animated:​ Bool)

Dismisses the standard interface used to customize the tab bar.

Deprecated

var selected​Image​Tint​Color:​ UIColor?

The tint color applied to the selected tab bar item.

Deprecated

Constants

UITab​Bar​Item​Positioning

Constants that specify tab bar item positioning.