Add button with custom image and action into navigationItem
How easily add a button with action to UINavigationItem, which is contained in UINavigationController
Adding simple UIBarButtonItem into UINavigationController
If you need to add a button to UINavigationController, it is quite simple, and you can do this with the following code
Objective-C
1 2 3 |
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:LOCS(@"Continue") style:UIBarButtonItemStyleBordered target:self action:@selector(myAction:)] autorelease]; |
Adding button with custom background image into UINavigationController
In this case, you must first create a button that will contain the necessary settings, and then add it to UINavigationController
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//create new button UIButton *button1 = [[UIButton alloc] init]; //set frame button1.frame=CGRectMake(0,0,51,30); //set background image [button1 setBackgroundImage:[UIImage imageNamed: @"logout_button.png"] forState:UIControlStateNormal]; //set target [button1 addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside]; //create new uibarbuttonitem from button UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:button1]; //set navigation item self.navigationItem.rightBarButtonItem = button; //release button [button1 release]; |
Posted on 10 April 2013