How to add custom image in navigation bar button item?

前端 未结 12 1693
我寻月下人不归
我寻月下人不归 2020-12-13 09:26
 UIBarButtonItem *doneitem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]autorelease];
           


        
相关标签:
12条回答
  • 2020-12-13 10:09

    Try this code:

    UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(sendmail)
         forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];
    
    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    self.navigationItem.rightBarButtonItem=mailbutton;
    [someButton release];
    
    0 讨论(0)
  • 2020-12-13 10:09

    Please try this code :

    UIButton *btnNext1 =[[UIButton alloc] init];
    [btnNext1 setBackgroundImage:[UIImage imageNamed:@"btnNext.png"] forState:UIControlStateNormal];
    
    btnNext1.frame = CGRectMake(100, 100, 50, 30);
    UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
    [btnNext1 addTarget:self action:@selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNext;
    
    0 讨论(0)
  • 2020-12-13 10:16
    UIButton *urButton = [UIButton buttonWithType:UIButtonTypeCustom];
    urButton.frame = urRequiredFrame;
    [urButton setImage:urImage forState:UIControlStateNormal];
    [urButton addTarget:self action:@selector(donePressed:)
         forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithCustomView:urButton];
    self.navigationItem.rightBarButtonItem=doneButton;
    
    0 讨论(0)
  • 2020-12-13 10:17
     UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"XXXXXXX.png"] 
                                                      style:UIBarButtonItemStylePlain 
                                                      target:self 
                                                      action:@selector(yourMethod)];
    
    self.navigationItem.rightBarButtonItem=_btn;
    
    0 讨论(0)
  • 2020-12-13 10:17

    To pick up on the answer by Gurpreet Singh. To keep the current status of an already existing button re-use the target and action.

    SEL selector = self.navigationItem.rightBarButtonItem.action;
    id target = self.navigationItem.rightBarButtonItem.target;
    UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_close.png"]
                                                          style:UIBarButtonItemStylePlain
                                                         target:target
                                                         action:selector];
    
    self.navigationItem.rightBarButtonItem = _btn;
    
    0 讨论(0)
  • 2020-12-13 10:18

    One simple way :

    UIImage* customImg = [UIImage imageNamed:@"custom-img.png"];
    
    UIBarButtonItem *_customButton = [[UIBarButtonItem alloc] initWithImage:customImg style:UIBarButtonItemStyleDone target:nil action:nil];
    
    self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:_customButton, nil];
    

    Then if you need more than one button you can just and an other UIBarButtonItem to initWithObjects.

    0 讨论(0)
提交回复
热议问题