简体   繁体   中英

Adding custom back button on navigation bar

I am trying to add custom back button on navigation bar but it is not working below is my code

-(void)addLeftButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

   [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

   self.navigationItem.backBarButtonItem = aBarButtonItem;
}  

Please tell what's wrong with this code

Try this.

-(void)addLeftButton
{
     UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

     [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

     aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

     UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

     [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

     [self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}

Here is the code that i have used to create custom backbutton

- (void)viewDidLoad
    {
        [super viewDidLoad];

    // Set the custom back button

    //add the resizable image
    UIImage *buttonImage = [[UIImage imageNamed:@"backbtn.png"]
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];

    MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
    lbl.text = @"Settings";
    lbl.backgroundColor = [UIColor clearColor];
    //lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lbl.numberOfLines = 0;

    lbl.lineHeight = 12;
    lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
    lbl.font = [UIFont fontWithName:@"Helvetica" size:12];

    [button addSubview:lbl];

    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;

}

-(void)back {
    // Tell the controller to go back
    [self.navigationController popViewControllerAnimated:YES];
}

This code will resize your image so it can fit the text. You can also put this button in any method.

You didn't add action to your button. So you need to add action to your button. For more info see in the below code..

-(void)addLeftButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

    [aButton addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----

   self.navigationItem.backBarButtonItem = aBarButtonItem;
}  

-(void)backBtnPressed
{
       [self.navigationController popViewControllerAnimated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM