How to create backBarButtomItem with custom view for a UINavigationController

前端 未结 14 933
余生分开走
余生分开走 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/

    0 讨论(0)
  • 2020-11-29 20:10

    Even though is already answered this worked for me:

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backArrow.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];
    

    BTW: even in iOS4 initializing my back button with initWithCustomView: didn't work for me. ;(

    0 讨论(0)
  • 2020-11-29 20:10

    I think I found the solution for this. Simply set the button on the navigation item on the previous controller (The one that you want to go back to)

    So if I have for example a root controller and I push a second controller and want to customize the back button then I should do the following:

    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
        initWithImage:[UIImage imageNamed:@"ico.png"] style:UIBarButtonItemStyleBordered
        target:nil action:nil] autorelease];
    

    Where self is the root view controller and not the second one.

    0 讨论(0)
  • 2020-11-29 20:11

    I'm fairly certain that the backBarButtonItem is a read-only property. Instead of modifying the backBarButtonItem, try setting a custom leftBarButtonItem and hide the backBarButtonItem:

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Prout" style:UIBarButtonItemStyleDone target:nil action:nil] autorelease];
    self.navigationItem.hidesBackButton = YES;
    

    You will also need to make sure you hook up the custom button to call the back action on the UINavigationBar.

    0 讨论(0)
  • 2020-11-29 20:11

    The navigationController's backBarButtonItem is set on the item whose title you're trying to affect.

    i.e. in Page 1's view controller, say, viewdidLoad:

    self.title = @"Page 1 of 4";
    
    self.navigationItem.backBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"Page 1"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil] autorelease];
    

    You would not override this in Page 2.

    Documentation for UINavigationItem : backBarButtonItem makes this clear:

    When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.

    0 讨论(0)
  • 2020-11-29 20:11

    My Logic:

    1. Create a custom button (aka a custom view subclass)
    2. Initialize a barbutton item with the custom button/view
    3. Add an action that allows us to "go back" to our previous view controller
    4. Set the left bar button item to this custom bar button item you created
    5. Hide the back bar button item of the view controller you're pushing to

    Step 3 was important. I figured the cleanest way to simulate the "go back" was to just utilize UINavigationController's method (popViewControllerAnimated:). So, I just add that action to the navigationController of the viewController I'm pushing (viewControllerToPush) like so:

    [navItemButton addTarget:viewControllerToPush.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    

    Example:

    UIViewController *viewControllerToPush = [[UIViewController alloc] init];
    UIImage *navImage = [UIImage imageNamed:@"your_back_button_image"];
    
    UIButton *navItemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [navItemButton setImage:navImage forState:UIControlStateNormal];
    [navItemButton setImage:navImage forState:UIControlStateHighlighted];
    [navItemButton setFrame:CGRectMake(0, 0, navImage.size.width, navImage.size.height)];
    
    [navItemButton addTarget:viewControllerToPush.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navItemButton];
    
    viewControllerToPush.navigationItem.leftBarButtonItem = barButtonItem;
    viewControllerToPush.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:viewControllerToPush animated:YES];
    
    0 讨论(0)
提交回复
热议问题