Lorenz Wöhr
Lorenz Wöhr

Reputation: 871

UIBarButtonItem - Change Background image programmatically

How can I change the background image of an UIBarButtonItem programmatically?

Upvotes: 0

Views: 3765

Answers (2)

thxou
thxou

Reputation: 691

Try just this:

[yourButton setBackgroundImage:[UIImage imageNamed:@"MY_IMAGE"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]

Change MY_IMAGE to your desired background image and for the forState argument, change it to whichever you want: normal, selected, disabled, etc.

Upvotes: -2

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

[yourbarButtonItem setBackButtonBackgroundImage:bckgrndImag forState:UIControlStateNormal barMetrics:0];

Details:

setBackButtonBackgroundImage:forState:barMetrics:

Sets the back button background image for a given control state and bar metrics

- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

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

– backButtonBackgroundImageForState:barMetrics:
– setBackButtonBackgroundVerticalPositionAdjustment:forBarMetrics:

Declared In

UIBarButtonItem.h

Upvotes: 4

Related Questions