0

In my app, i'm using a custom UINavigationBar and custom UIBarButtonItems in a NavigationController. My (custom) NavigationBar looks fine, but when I want to place my own backBarButtonItem and RightBarButtonItem, it goes wrong.

Installing my own backBarButtonItem doesn't work at all (after a PushViewController method). I use this code:

    UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];  
    UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];

    [home setBackgroundImage:homeImage forState:UIControlStateNormal];  

    home.frame = CGRectMake(0, 0, 51, 30);  
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]  
                                     initWithCustomView:home];  


    [self.navigationItem setBackBarButtonItem:backButton];

I also have my own rightBarButtonItem. I am able to get it on my NavigationBar, but it isn't calling the method I want to do it. Here's my code for that one:

    UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];  
    UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];

    [home setBackgroundImage:homeImage forState:UIControlStateNormal];  

    home.frame = CGRectMake(0, 0, 51, 30);  
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]  
                                     initWithCustomView:home];  
    // self.navigationItem.backBarButtonItem = cancelButton;  

    [self.navigationItem setBackBarButtonItem:cancelButton];

After a couple of hours looking on the internet for a solution with no result, I hope that you guys can help me!

Thanks!

EDIT: Sorry posted the wrong code for the last issue:

UIButton *home2 = [UIButton buttonWithType:UIButtonTypeCustom];  
[home2 setTitle:@"Sort" forState:UIControlStateNormal];
[home2.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];

UIImage *homeImage2 = [UIImage imageNamed:@"Topbarbutton"];

[home2 setBackgroundImage:homeImage2 forState:UIControlStateNormal];  

home2.frame = CGRectMake(0, 0, 77, 30);  
UIBarButtonItem *cancelButton2 = [[UIBarButtonItem alloc]  
                                 initWithCustomView:home2];
[cancelButton2 setTarget:self];
[cancelButton2 setAction:@selector(sorteren)];



self.navigationItem.rightBarButtonItem = cancelButton2;
Jelle
  • 284
  • 3
  • 13
  • If I get the situation correctly, the NavigationBar shows the leftBarButton and rightBarButton correctly in the Root View Controller until you push another View Controller? – barley Dec 30 '11 at 20:28

2 Answers2

0

Hm, seems you want to add add a Button to the Bottom toolbar? That's how you could do that:

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Title" target:self action:@selector(myButtonClicked:)];
    [toolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
Dion
  • 3,145
  • 20
  • 37
0

There is a catch with the backBarButtonItem you set it on the a UIViewController, but it won't show up when that view is visible, it will show up only when that view is the one to go back to.

backBarButtonItem
The bar button item to use when a back button is needed on the navigation bar.
@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem
Discussion
When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
The default value of this property is nil.


And for the method not being called you need to set the @selector to be called

You could use this method to create your button

- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

Edit
For the back button
Don't create a UIButton, go for the UIBarButton directly.
Here is how I create a backBarButtonItem.

UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Maison.png"]
                                                        style:UIBarButtonItemStylePlain
                                                       target:nil
                                                       action:nil];
self.navigationItem.backBarButtonItem = bbi;
[bbi release], bbi = nil;
Vincent Bernier
  • 8,674
  • 4
  • 35
  • 40