Programmatically changing the identifier property of UIBarButtonItem
-
12-09-2019 - |
Question
Through Interface Builder I have the ability to change the Identifier of a UIBarButtonItem to something like "Add" (or "Undo", "Redo" etc...). This gives my button a nice "+" image.
How can I set this programatically? The UIBarButtonItem does not accept a "setIdentifier" message.
Solution
Once constructed, a UIBarButtonItem's "Identifier" can not be modified. However, the UI can be changed by replacing the button with a programmatically constructed variant. For example:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(doAddAction:)];
OTHER TIPS
If you want one button style: bordered and identifier: camera use
UIBarButtonItem *btn;
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(shotAction:)];
btn.style=UIBarButtonItemStyleBordered;
Once the UIBarButtonItem is created, there is no way to change the identifier. However, you can create a new UIBarButtonItem to replace the old UIBarButtonItem
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(buttonClickedAction:)];
self.navigationItem.rightBarButtonItem = barButton;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow