UIBarButtonItem Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UIBarButtonItem.h |
Overview
A bar button item is a button specialized for placement on a UIToolbar or UINavigationBar object. It inherits basic button behavior from its abstract superclass, UIBarItem. The UIBarButtonItem defines additional initialization methods and properties for use on toolbars and navigation bars.
You typically use Interface Builder to create and configure bar button items.
Customizing Appearance
You use the methods listed in “Customizing Appearance” to customize the appearance of buttons. You send the setter messages to the appearance proxy ([UIBarButtonItem appearance]) to customize all buttons, or to a specific UIBarButtonItem instance. You may use customized buttons in standard places in a UINavigationItem object (backBarButtonItem, leftBarButtonItem, rightBarButtonItem) or a UIToolbar instance.
In general, you should specify a value for the normal state to be used by other states which don’t have a custom value set. Similarly, when a property is dependent on the bar metrics (on iPhone, in landscape orientation bars have a different height from standard), you should specify a value of UIBarMetricsDefault.
Tasks
Initializing an Item
-
– initWithBarButtonSystemItem:target:action: -
– initWithCustomView: -
– initWithImage:style:target:action: -
– initWithTitle:style:target:action: -
– initWithImage:landscapeImagePhone:style:target:action:
Getting and Setting Properties
-
targetproperty -
actionproperty -
styleproperty -
possibleTitlesproperty -
widthproperty -
customViewproperty
Customizing Appearance
-
tintColorproperty -
– backButtonBackgroundImageForState:barMetrics: -
– setBackButtonBackgroundImage:forState:barMetrics: -
– backButtonTitlePositionAdjustmentForBarMetrics: -
– setBackButtonTitlePositionAdjustment:forBarMetrics: -
– backButtonBackgroundVerticalPositionAdjustmentForBarMetrics: -
– setBackButtonBackgroundVerticalPositionAdjustment:forBarMetrics: -
– backgroundVerticalPositionAdjustmentForBarMetrics: -
– setBackgroundVerticalPositionAdjustment:forBarMetrics: -
– backgroundImageForState:barMetrics: -
– setBackgroundImage:forState:barMetrics: -
– backgroundImageForState:style:barMetrics: -
– setBackgroundImage:forState:style:barMetrics: -
– titlePositionAdjustmentForBarMetrics: -
– setTitlePositionAdjustment:forBarMetrics:
Properties
action
The selector defining the action message to send to the target object when the user taps this bar button item.
Discussion
If the value of this property is NULL, no action message is sent. The default value is NULL.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIBarButtonItem.hcustomView
A custom view representing the item.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hpossibleTitles
The set of possible titles to display on the bar button.
Discussion
Use this property to provide a hint to the system on how to correctly size the bar button item to be wide enough to accommodate your widest title. Set the value of this property to an NSSet object containing all the titles you intend as possible titles for the bar button item. Use the actual text strings you intend to display.
This property applies to bar button items placed on navigation bars or toolbars.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hstyle
The style of the item.
Discussion
One of the constants defined in UIBarButtonItemStyle. The default value is UIBarButtonItemStylePlain.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.htarget
The object that receives an action when the item is selected.
Discussion
If nil, the action message is passed up the responder chain where it may be handled by any object implementing a method corresponding to the selector held by the action property. The default value is nil.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.htintColor
The tint color for the button item.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hwidth
The width of the item.
Discussion
If this property value is positive, the width of the combined image and title are fixed. If the value is 0.0 or negative, the item sets the width of the combined image and title to fit. This property is ignored if the style uses radio mode. The default value is 0.0.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hInstance Methods
backButtonBackgroundImageForState:barMetrics:
Returns the back button background image for a given control state and bar metrics.
Parameters
- state
A control state.
- barMetrics
Bar metrics.
Return Value
The back button background image for state and barMetrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
UIBarButtonItem.hbackButtonBackgroundVerticalPositionAdjustmentForBarMetrics:
Returns the back button vertical position offset for given bar metrics.
Parameters
- barMetrics
Bar metrics.
Return Value
The back button vertical position offset for barMetrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
This offset is used to adjust the vertical centering of bordered bar buttons within the bar.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hbackButtonTitlePositionAdjustmentForBarMetrics:
Returns the back button title offset for given bar metrics.
Parameters
- barMetrics
Bar metrics.
Return Value
The back button title offset for barMetrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hbackgroundImageForState:barMetrics:
Returns the background image for a given state and bar metrics.
Parameters
- state
A control state.
- barMetrics
Bar metrics.
Return Value
The background image for the button given state and metrics.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hbackgroundImageForState:style:barMetrics:
Returns the background image for the specified state, style, and metrics.
Parameters
- state
The bar button state.
- style
The bar button style.
- barMetrics
The bar button metrics.
Return Value
The background image associated with the specified state, style, and metrics.
Availability
- Available in iOS 6.0 and later.
Declared In
UIBarButtonItem.hbackgroundVerticalPositionAdjustmentForBarMetrics:
Returns the background vertical position offset for given bar metrics.
Parameters
- barMetrics
Bar metrics.
Return Value
The background vertical position offset for barMetrics.
Discussion
This offset is used to adjust the vertical centering of bordered bar buttons within the bar.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hinitWithBarButtonSystemItem:target:action:
Initializes a new item containing the specified system item.
Parameters
- systemItem
The system item to use as the first item on the bar. One of the constants defined in
UIBarButtonSystemItem.- target
The object that receives the action message.
- action
The action to send to target when this item is selected.
Return Value
A newly initialized item containing the specified system item. The item’s target is nil.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hinitWithCustomView:
Initializes a new item using the specified custom view.
Parameters
- customView
A custom view representing the item.
Return Value
Newly initialized item with the specified properties.
Discussion
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hinitWithImage:landscapeImagePhone:style:target:action:
Initializes a new item using the specified images and other properties.
Parameters
- image
The item’s image. If
nilan image is not displayed.- landscapeImagePhone
The image to be used for the item in landscape bars in the
UIUserInterfaceIdiomPhoneidiom.- style
The style of the item. One of the constants defined in
UIBarButtonItemStyle.- target
The object that receives the action message.
- action
The action to send to target when this item is selected.
Return Value
A new item initialized to use using the specified images and other properties
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hinitWithImage:style:target:action:
Initializes a new item using the specified image and other properties.
Parameters
- image
The item’s image. If
nilan image is not displayed.The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points. The alpha values in the source image are used to create the images—opaque values are ignored.
- style
The style of the item. One of the constants defined in
UIBarButtonItemStyle.- target
The object that receives the action message.
- action
The action to send to target when this item is selected.
Return Value
Newly initialized item with the specified properties.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hinitWithTitle:style:target:action:
Initializes a new item using the specified title and other properties.
Parameters
- title
The item’s title. If
nila title is not displayed.- style
The style of the item. One of the constants defined in
UIBarButtonItemStyle.- target
The object that receives the action message.
- action
The action to send to target when this item is selected.
Return Value
Newly initialized item with the specified properties.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hsetBackButtonBackgroundImage:forState:barMetrics:
Sets the back button background image for a given control state and bar metrics
Parameters
- backgroundImage
The image to use for the back button’s background.
- state
A control state.
- barMetrics
Bar metrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
For good results, backgroundImage must be a stretchable image.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
UIBarButtonItem.hsetBackButtonBackgroundVerticalPositionAdjustment:forBarMetrics:
Sets the back button vertical position offset for given bar metrics.
Parameters
- adjustment
The back button vertical position offset for barMetrics.
- barMetrics
Bar metrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
This offset is used to adjust the vertical centering of bordered bar buttons within the bar.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hsetBackButtonTitlePositionAdjustment:forBarMetrics:
Sets the back button title offset for given bar metrics
Parameters
- adjustment
The back button title offset for barMetrics.
- barMetrics
Bar metrics.
Discussion
This modifier applies only to navigation bar back buttons and is ignored by other buttons.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hsetBackgroundImage:forState:barMetrics:
Sets the background image for a given state and bar metrics.
Parameters
- backgroundImage
The background image for the specified state and metrics.
- state
A control state.
- barMetrics
Bar metrics.
Discussion
For good results, backgroundImage must be a stretchable image.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hsetBackgroundImage:forState:style:barMetrics:
Sets the background image for the specified state, style, and metrics.
Parameters
- backgroundImage
The background image to use for the specified state, style, and metrics.
- state
The bar button state.
- style
The bar button style.
- barMetrics
The bar button metrics.
Discussion
For good results, backgroundImage must be a stretchable image.
Availability
- Available in iOS 6.0 and later.
Declared In
UIBarButtonItem.hsetBackgroundVerticalPositionAdjustment:forBarMetrics:
Sets the background vertical position offset for given bar metrics.
Parameters
- adjustment
The background vertical position offset for barMetrics.
- barMetrics
Bar metrics.
Discussion
This offset is used to adjust the vertical centering of bordered bar buttons within the bar.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hsetTitlePositionAdjustment:forBarMetrics:
Sets the title offset for given bar metrics.
Parameters
- adjustment
The title offset for barMetrics.
- barMetrics
Bar metrics.
Discussion
This offset is used to adjust the position of a title (if any) within a bordered bar button.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.htitlePositionAdjustmentForBarMetrics:
Returns the title offset for given bar metrics.
Parameters
- barMetrics
Bar metrics.
Return Value
The title offset for barMetrics.
Discussion
This offset is used to adjust the position of a title (if any) within a bordered bar button.
Availability
- Available in iOS 5.0 and later.
Declared In
UIBarButtonItem.hConstants
UIBarButtonSystemItem
Defines system-supplied images for bar button items.
typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo, // iOS 3.0 and later
UIBarButtonSystemItemRedo, // iOS 3.0 and later
UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
} UIBarButtonSystemItem;
Constants
UIBarButtonSystemItemDoneThe system Done button. Localized.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemCancelThe system Cancel button. Localized.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemEditThe system Edit button. Localized.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemSaveThe system Save button. Localized.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemAddThe system plus button containing an icon of a plus sign.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemFlexibleSpaceBlank space to add between other items. The space is distributed equally between the other items. Other item properties are ignored when this value is set.
Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemFixedSpaceBlank space to add between other items. Only the
widthproperty is used when this value is set.Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemComposeThe system compose button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemReplyThe system reply button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemActionThe system action button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemOrganizeThe system organize button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemBookmarksThe system bookmarks button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemSearchThe system search button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemRefreshThe system refresh button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemStopThe system stop button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemCameraThe system camera button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemTrashThe system trash button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemPlayThe system play button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemPauseThe system pause button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemRewindThe system rewind button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemFastForwardThe system fast forward button.

Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemUndoThe system undo button.

Available in iOS 3.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemRedoThe system redo button.

Available in iOS 3.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonSystemItemPageCurlThe system page curl button.

This bar button image can be used only for bar button items placed on toolbars.
Available in iOS 4.0 and later.
Declared in
UIBarButtonItem.h.
UIBarButtonItemStyle
Specifies the style of a item.
typedef enum {
UIBarButtonItemStylePlain,
UIBarButtonItemStyleBordered,
UIBarButtonItemStyleDone,
} UIBarButtonItemStyle;
Constants
UIBarButtonItemStylePlainGlows when tapped. The default item style.
Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonItemStyleBorderedA simple button style with a border.
Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.UIBarButtonItemStyleDoneThe style for a done button—for example, a button that completes some task and returns to the previous view.
Available in iOS 2.0 and later.
Declared in
UIBarButtonItem.h.
Availability
- Available in iOS 2.0 and later.
Declared In
UIBarButtonItem.hUIBarMetrics
Constants to specify metrics to use for appearance.
typedef enum {
UIBarMetricsDefault,
UIBarMetricsLandscapePhone,
} UIBarMetrics;
Constants
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)