Right Bar Button item not working
-
17-02-2021 - |
Question
Actually i am placing a bar button on right side of navigation bar, it's not working. But when i use it as left bar button item, it's working fine. I am using ios5.
It was also not working when i have both button left and right bar button. Then i set the frame for both, then these are working. But when i have only one button on right side, it's not working.
UIButton *but1 = [UIButton buttonWithType:UIButtonTypeCustom];//customising map button.
but1.frame = CGRectMake(270,0,50,40);
[but1 addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];//on cilcking an map button clicked method is called.
buttonRight = [[UIBarButtonItem alloc]initWithCustomView:but1];//setting map button on Navigation bar.
self.navigationItem.rightBarButtonItem = buttonRight;//setting button on the Right of navigation bar.
how to trace out this error?
Solution
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 50, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:0];
// create a standard BarButtonItem
UIBarButtonItem *SettingsBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_setting.png"] style:UIBarButtonItemStylePlain
target:self
action:@selector(ActionMethod:)];
[buttons addObject:SettingsBtn];
[SettingsBtn release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
[toolbar release];
OTHER TIPS
UIBarButtonItem *addButton=[[UIBarButtonItem alloc]initWithTitle:@"edit" style:UIBarButtonItemStyleBordered target:self action:@selector(edit_details)];
self.navigationItem.rightBarButtonItem=addButton;
Check it Wil work
For Swift 4.2 code:
In the View did load function
let addButton = UIBarButtonItem(title: "edit", style: .plain, target: self, action: #selector(edit))
navigationItem.rightBarButtonItem = addButton
Function definition goes here:
@objc func edit() {
// Body definition
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow