3

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 *)#>]
memmons
  • 40,222
  • 21
  • 149
  • 183
Pedroinpeace
  • 1,419
  • 1
  • 14
  • 20

1 Answers1

3

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/

memmons
  • 40,222
  • 21
  • 149
  • 183
  • thanks for fast response. I think I mention in my post even that I have tried and succeeded with your approach. But I lose the possibility to add a background.png and attach an icon.png on top :( Sooo strange becaus this works with leftBarButton and backBarButton seems to have same properties tied to it. `[self.navigationItem setLeftBarButtonItem:<#(UIBarButtonItem *)#>]` Maby I have reached a dead end? – Pedroinpeace Mar 12 '13 at 15:43
  • Not sure what you mean. You are trying to replace the leftBarButtonItem while this approach uses the appearance proxy along with the UIBarMetricsLandscapePhone bar metrics. More info here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html – memmons Mar 12 '13 at 16:01
  • After rereading your question again, I think I see what you are trying to do. You are trying to replace the back button with your own custom button while also keeping the special transitions that the back button has. This isn't possible. You can change the appearance and keep the transitions or you can change the class and lose the transitions. – memmons Mar 12 '13 at 16:35
  • In any case, why do you need a custom class that allows you to set the background and foreground image? Just combine the two images and use the appearance proxy to set it. – memmons Mar 12 '13 at 16:37
  • from the beginning I actually started posting another question about masking the very same button you are so correct to refer to. I wanted to mask only the icon.png. So that I could reuse the same icon with different colorschemes set by ios. So I know now what I can and can not do with the backBarButton and it helped a lot and actually made a lot sence reading your answer. Maby you also know the answer to the mask q? If I would to make the approach by implementing my button to leftBarButton instance. I have read about tintColoring but doesnt seem to be able to do mask in navbar – Pedroinpeace Mar 12 '13 at 18:38