Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created May 17, 2011 06:11
Show Gist options
  • Select an option

  • Save chrishulbert/976032 to your computer and use it in GitHub Desktop.

Select an option

Save chrishulbert/976032 to your computer and use it in GitHub Desktop.
Centered UIButton in a navigation bar on the iphone
UIButton* centeredNavBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
centeredNavBarButton.frame = CGRectMake(0, 0, 150, 31);
[centeredNavBarButton setBackgroundImage:[UIImage imageNamed:@"mybutton_normal"] forState:UIControlStateNormal];
[centeredNavBarButton setBackgroundImage:[UIImage imageNamed:@"mybutton_selected"] forState:UIControlStateSelected];
[centeredNavBarButton setTitle:@"Blah blah" forState:UIControlStateNormal];
[centeredNavBarButton addTarget:self action:@selector(myActionToCallWhenTapped) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = centeredNavBarButton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment