Advertisement
priore

Two or more UIButton on UINavigationBar

Sep 7th, 2012
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. NSMutableArray* buttons = [[[NSMutableArray alloc] initWithCapacity:3] autorelease];
  2.     
  3. UIButton * button1 = [UIButton buttonWithType:UIButtonTypeCustom];
  4.         [button1 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
  5.         [button1 addTarget:self action:@selector(firstMethod:) forControlEvents:UIControlEventTouchUpInside];
  6.         [button1 setImage:[UIImage imageNamed:@"first-button.png"] forState:UIControlStateNormal];
  7.         UIBarButtonItem * buttonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
  8.     [buttons addObject:buttonItem1];
  9.     
  10. UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
  11.         [button2 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
  12.         [button2 addTarget:self action:@selector(secondMethod:) forControlEvents:UIControlEventTouchUpInside];
  13.         [button2 setImage:[UIImage imageNamed:@"second-button.png"] forState:UIControlStateNormal];
  14.         UIBarButtonItem * buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
  15.     [buttons addObject:buttonItem2];
  16.     
  17.     float barWidth = button1.frame.size.width + button2.frame.size.width;
  18.     UIToolbar* tbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, barWidth * 1.5, 44.01)] autorelease];
  19.     [tbar setItems:buttons];
  20.     
  21. UIImage *imgBack = [UIImage imageNamed:@"image-back.png"];
  22.     UIImageView *tbarBg = [[[UIImageView alloc] initWithImage: imgBack] autorelease];
  23.     tbarBg.backgroundColor = [UIColor redColor];
  24.     [tbar insertSubview:tbarBg atIndex:tbar.subviews.count == 0 ? 0 : 1];
  25.     
  26.     self.navigationController.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tbar];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement