How to create backBarButtomItem with custom view for a UINavigationController

前端 未结 14 1137
余生分开走
余生分开走 2020-11-29 19:39

I have a UINavigationController into which I push several views. Inside viewDidLoad for one of these views I want to set the self.navigationI

14条回答
  •  清酒与你
    2020-11-29 20:09

    As of iOS5 we have an excellent new way of customizing the appearance of almost any control using the UIAppearance protocol, 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/

提交回复
热议问题