0

I have a UINavigationBarthat I have manually dragged and added to a ViewController in my storyboard. Within that NavigationBar I have dragged and added a "UIBarButtonItem" styled as the default "Add" image.

The view is pushed modally, so it does not have a NavigationBar that is automatically pushed with it.

Both the navBar and the BarButton are connected to my viewcontroller and the storyboard like so

@property (strong, nonatomic) IBOutlet UINavigationBar *RibbonDetailNavBar;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *addRemoveButton;

I try to change the button image to another default, the Trash icon, in viewDidLoad like so

if (alreadyAdded)
{
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonSystemItemTrash];
}

Which had no effect.

I have also tried this:

if (alreadyAdded)
{
    _addRemoveButton = nil;
}

Which also had no effect

and also this:

if (alreadyAdded)
{
    _addRemoveButton = [[UIBarButtonItem alloc]initWithImage:nil style:UIBarButtonSystemItemTrash target:nil action:nil];
}

which also had no effect.

How do I set this button to the trash icon?

TestinginProd
  • 1,085
  • 1
  • 15
  • 35

1 Answers1

1

UIBarButtonSystemItemTrash cannot be retrieved by style property. style is of type UIBarButtonItemStyle which can be UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered

Look into the documentation here

Use this code

@property (strong, nonatomic) IBOutlet UINavigationBar *RibbonDetailNavBar;
// No need to create rightBarButtonItem IBOutlet.
if (alreadyAdded)
{
    self.RibbonDetailNavBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemTrash target: nil action: nil];
}
else {
    self.RibbonDetailNavBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: nil action: nil];
}

Hope it helps you. Happy coding! :)

Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43