uinavigationcontrolleruibuttonuibarbuttonitemuitoolbartoolbaritems

uinavigationController's toolbar with custom items


I am trying to use the NavigationController's toolbar in my app. This toolbar's toolbarItems are suppose to change depending on which view controller is presented. this is very basic.

What I am trying to do is to add custom buttons to the toolbar using the UIBarButtonItem's "initWithCustomView:" method. However, the button won't show up on the toolbar. But if I create the UIBarButtonItem using the "initWithTitle:" or "initWithBarButtonSystemItem:" method, the button show up. For example, look at the code below:

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];

NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil];
[self setToolbarItems:array];

If this is done, buttons show up on the toolbar. But, if I were to do the following:

UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
replyBtn.frame = CGRectMake(10, 0, 40, 40);
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];

NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil];
[self setToolbarItems:array];

In this code, only the item1 and item2 are displayed on the toolbar. replyButton is not shown on the toolbar. There is blank space at the place where the button is suppose to be at.

If this same code used on a regular toolbar that I create instead of NavigationController's toolbar, the button shows up. I am trying to just use one toolbar throughout the app to have the same feel that Apple's Mail application does. The reason that I need to use the "initWithCustomView:" method is because one of the icons is colored and this is the only way it shows up colored on a normal toolbar. Now, I have looked through apple documentation and there isn't any mention of why the "initWithCustomView:" method couldn't be called (or maybe I couldn't find it).

Could please somebody shine some light on this topic to help me point in the right direction. thanks in advance guys.


Solution

  • I can't see the difference from what you tried, but it eventually worked for me, with that code:

     //////////////////////////////////////////////////////////////////////////////////////////////
    /////>>>> Adding buttons for browsing in the toolbar of the popover's navigation controller///
    //////////////////////////////////////////////////////////////////////////////////////////////
    
    //>>>>Create a goBack button    
    goBack = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *goBackImage = [UIImage imageNamed:@"back1.png"];
    [goBack setImage:goBackImage forState:UIControlStateNormal];
    [goBack addTarget:self action:@selector(goBackClicked:) forControlEvents:UIControlEventTouchUpInside];
    goBack.frame = CGRectMake(0,0,goBackImage.size.width,goBackImage.size.height);
    //Create a Bar button to hold this button
    UIBarButtonItem *goBackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:goBack] autorelease]; 
    
    UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil] autorelease];
    NSArray *arrayOfButtons = [[[NSArray alloc] initWithObjects:goBackBarButton, flex1, addButton, nil] autorelease];
    [self setToolbarItems:arrayOfButtons];
    

    Note few differences from yours (maybe that's where the catch? I'm not sure): 1. my buttons are not allocated locally in the method, but in the class (you know, the property, synthesize, etc) 2. yours

    [replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];
    

    where mine looks a bit different

    [goBack setImage:goBackImage forState:UIControlStateNormal];
    

    Try these tiny changes, maybe it will work :)