title: Add button to navigationbar programmatically
body: <p>Hi I need to set the button on right side, in navigation bar, programatically , so that if I press the button I will perform some actions.  I have created the navigation bar, programatically by;</p>
  
  <pre><code>navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
  </code></pre>
  
  <p>Similarly, I need to add the button, on the right side of this navigation bar. For that I used, </p>
  
  <p><strong>1.</strong> &nbsp;</p>
  
  <pre><code>    UIView* container = [[UIView alloc] init];
  
      // create a button and add it to the container
      UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)];
      [container addSubview:button];
      [button release];
  
      // add another button
      button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)];
      [container addSubview:button];
      [button release];
  
      // now create a Bar button item
      UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
  
      // set the nav bar's right button item
      self.navigationItem.rightBarButtonItem = item;
      [item release];
  </code></pre>
  
  <p><strong>2.</strong> &nbsp;</p>
  
  <pre><code>    UIImage *im;
      im=[UIImage imageNamed:@"back.png"];
      [button setImage:im forState:UIControlStateNormal];
      [im release];
      backButton = [[UIBarButtonItem alloc] initWithCustomView:button];       
      [backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)];
      [self.navigationItem setRightBarButtonItem:backButton];
  </code></pre>
  
  <p><strong>3.</strong> &nbsp;</p>
  
  <pre><code>    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button"               style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];
      self.navigationItem.rightBarButtonItem = refreshItem;
  
      [refreshItem release];
  </code></pre>
  
  <p>I tried all these ways, but none is displaying the button on the right hand side.</p>