views:

151

answers:

2

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.

A: 

I'd wager a guess that the item isn't showing up because its view doesn't have anything in it. Are you sure there's an image called Reply_Message.png in your project? [UIImage imageNamed:@"Reply_Message.png"] might be nil.

jtbandes
i am positive about that. If i take the same exact code and set those Items to a toolbar that I create, the button shows up. This is not the issue for sure. I have even tried a UIButton with just a title, instead of the image, add that as a subview to the UIBarButtonItem and even that won't show up.
Bittu
A: 

You can use one toolbar throughout your app without using the navigation controllers toolbar. Since your code works with a non-navigationController toolbar, this might be the easiest way to accomplish what you want. In your app delegate, try adding a toolbar to window, like this. This way it is persistent throughout the app. Make sure you consider a scheme that will let you access the toolbar to add/remove buttons, or hide/show it from different view controllers.

    - (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    /////// frame sizes ////////
    // compensate for the status bar
    int statusBarHeight = 20;

    // toolbar
    int toolbarWidth   = window.frame.size.width;
    int toolbarHeight  = 30;  // I think standard size is 30
    int toolbarxOffset = 0;
    int toolbaryOffset = window.frame.size.height - tHeight;
    CGRect toolbarFrame = CGRectMake(toolbarxOffset,
                                     toolbaryOffset,
                                     toolbarWidth,
                                     toolbarHeight);

    /////// toolbar //////////////////////
    self.myPersistentToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
    [window addSubview:self.myPersistentToolbar];
}
Drewsmits
Thank you for your reply Drew. I had contemplated this idea earlier. If I had a app that used the navigationController as its rootViewController, this might have worked. Rather, I am using this Three20 library's launcher as the root view, with each "sub-app" containing its own settings. Some have tabbars in them, while others have navigationController or splitviewController. I was trying to avoid adding anything to the window itself due to this reason. the solution I came up with was to add my own toolbar to as a subview to navigationController's view.
Bittu