Pedroinpeace
Pedroinpeace

Reputation: 1439

iOS navigationbar setBackBarButtonItem to custom button

I need some assistance with how to customize 'backBarButtonItem' And no I dont want to use the LeftBarButtomItem since I want to inherit the style and keep the transition from the 'BackBarButtom'

Okey, so what I got is:

UIBarButtonItem* barbtnItem = [[UIBarButtonItem alloc]initWithCustomView:  [ArrivalProto ArrivalBtnTypeBack]];

[ArrivalProto ArrivalBtnTypeBack] gives me back a custom UIbutton with both a setBackgroundImage an a setImage.

And then all I want to do is to add this to customize the BackBarButton:

[self.navigationItem setBackBarButtonItem: barbtnItem];

But nooo. Just the the plain normal backbutton :((( While this seems to work:

//self.navigationItem setLeftBarButtonItem:barbtnItem];

And this as well:

UIImage *btnTrnspBgrImg30 = [[UIImage imageNamed:@"trspBlackBtn30"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]; 
 [[UIBarButtonItem appearance] setBackButtonBackgroundImage:btnTrnspBgrImg30      forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Info:

[self.navigationItem setBackBarButtonItem: <#(UIBarButtonItem *)#>];
[self.navigationItem setLeftBarButtonItem:<#(UIBarButtonItem *)#>]

Upvotes: 3

Views: 2070

Answers (1)

memmons
memmons

Reputation: 40502

As of iOS5 we have an excellent new way of customizing the appearance of almost any control using the appearance proxy, i.e. [UIBarButtonItem appearance]. The appearance proxy allows you to create application wide changes to the look of controls. Below is an example of a custom back button created with the appearance proxy.

enter image description here

Use the example code below to create a back button with custom images for normal and highlighted states. Call the following method from you appDelegate's application:didFinishLaunchingWithOptions:

- (void) customizeAppearance {

UIImage *i1 = [[UIImage imageNamed:@"custom_backButton_30px"]
                      resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];
UIImage *i2 = [[UIImage imageNamed:@"custom_backButton_24px"] 
                      resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 6)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1 
                              forState:UIControlStateNormal 
                              barMetrics:UIBarMetricsDefault];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2 
                              forState:UIControlStateNormal 
                              barMetrics:UIBarMetricsLandscapePhone];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i1
                              forState:UIControlStateHighlighted 
                              barMetrics:UIBarMetricsDefault];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:i2 
                              forState:UIControlStateHighlighted 
                              barMetrics:UIBarMetricsLandscapePhone];
}

This is just a quick example. Normally you would want to have separate images for normal and highlighted (pressed) state.

If you are interested in customizing the appearance of other controls, some good examples can be found here: http://ios.biomsoft.com/2011/10/13/user-interface-customization-in-ios-5/

Upvotes: 3

Related Questions