Can't get the icon for UIBarButtonSystemItemTrash - ios

In order to get the trash image displayed in a standard toolbar on iOS. I am using the following code:
UIBarButtonItem *tempTBButn=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
UIImage *trashImg=tempTBButn.image;
But it does not work. The result I get in trashImg is just nil.
What should I do to obtain the result I want? That is to have the trash icon in trashImg.

The image property is only set when you create a UIBarButtonItem with one of the custom image init methods like:
UIBarButtonItem *customImageBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"yourImage"] style:UIBarButtonItemStylePlain target:self action:#selector(barButtonItemPressed:)];
The image property defaults to nil when you use the initWithBarButtonSystemItem: method. Your best bet is to just use a trash icon of your own, or work with just having the standard icon on the nav.

Related

iOS/Objective-C: Add badge to custom UIBarButtonItem

This is now very common but I cannot find a way to add a badge to a custom, or non-system, UIBarButton Item.
The following will add a badge to a systemUIBarButtonItem. But it does not work for one created from an image, i.e. a custom one. I would like to avoid creating a dependency on a separate class and would really like to find something similar to this that works for a custom button. Thanks in advance for any suggestions.
This creates badge for systemButton such as a search button:
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchButtonPressed)];
int badgeint = 4;
NSString *badgestring = [NSString stringWithFormat:#"%d",badgeint];
self.navigationItem.rightBarButtonItems = #[searchButton];
self.navigationItem.rightBarButtonItem.badgeValue = badgestring;
The same code for a custom button--there is no system button for notification so I need to use a custom image--does not produce the badge.
UIBarButtonItem *notificationButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"notification-32.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(notificationButtonPressed)];
int badgeint = 4;
NSString *badgestring = [NSString stringWithFormat:#"%d",badgeint];
self.navigationItem.rightBarButtonItems = #[notificationButton];
self.navigationItem.rightBarButtonItem.badgeValue = badgestring;
Note: the reason I have barbuttonitems is that sometimes I use more than one)
Thanks for any suggestions
EDIT:
I found out that I made a stupid error--I set the value two different times. I thought the second would override the first, but apparently it confused it. There is no difference between system and custom buttons. I did discover, however, that if you have multiple buttons, the badge will always appear at the right of the group. Using this method, you can't seem to put the badge on a bar button item second in from right for example.

iOS Bar Item image displaying wrong color

I have a bar with Bar Items, my .png image has green color, but when i add it to storyboard it's displaying as blue.
How can i make it display the image as it is?
Use tintColor of UIBarButton to set the desired color for the image.
If its absolutely necessary to use original image colors, use this to set the image:
[aBarButton setImage:[[UIImage imageNamed:#"xyz.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
The docs are a little ambiguous about this
The images displayed on the bar are derived from this image. If this
image is too large to fit on the bar, it is scaled to fit. Typically,
the size of a toolbar and navigation bar image is 20 x 20 points. The
alpha values in the source image are used to create the images—opaque
values are ignored.
Essentially what this is saying is the image you supply will not be what is actually displayed. Instead the system uses the alpha mask of the image and the tintColor of the item to generate the final display.
add image programmatically
[button setImage:[[UIImage imageNamed:#"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
if its not work then try this:-
UIImage *myImage = [UIImage imageNamed:#"myImageFile.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:#selector(menuObject:)];
self.navigationItem.leftBarButtonItem = menuButton;
if its not work then try this:-
#define setTurqoiseColor [UIColor colorWithRed:68.0f/255.0f green:181.0f/255.0f blue:223.0f/255.0f alpha:1.0]
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStyleBordered target:self action:#selector(toggleMenu)];
menuButton.tintColor = setTurqoiseColor;
To set tint color of bar item global, in your App Delegate, add these lines of code
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // set to your color
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
You have to set your UITabBar tintColor.
If you would like to add a custom color / gradient you can set your tabBarItem image and selectedImage property as follow:
customTabBarItem.selectedImage = UIImage(named: "customSelectedImage")!.imageWithRenderingMode(.AlwaysOriginal)
customTabBarItem.image = UIImage(named: "customUnselectedImage")!.imageWithRenderingMode(.AlwaysOriginal)

UIBarButtonItem Custom Icon

I use the following line to create a UIBarButtonItem with a custom icon named import2x.png:
UIBarButtonItem *btnImport = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"import2x.png"] style:UIBarButtonItemStylePlain target:self action:#selector(btnImport)];
The icon is a png that looks like this:
But here's how it looks when it is run on the simulator - a solid red square:
The icon is created using a transparent (alpha=0) background.
Any suggestions? TIA.
The image in the link provided has an opaque background. If that is the same image you're using that's why it look's like a solid block.
It should be created with a transparent background, like this:
i.e. in Photoshop you should be able to see the background pattern like this:
Try this one. Its working fine. I also share my output screenshot. u have to use small size of icon . but icon should be visible, no problem.
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Login-pin.png"] landscapeImagePhone:[UIImage imageNamed:#"Login-pin.png"] style:UIBarButtonItemStylePlain target:self action:#selector(PickerCancelClick:)];
The search image is my output.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"g122.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuClieckd:)];
This worked for me

weird background behind navigation bar button item created from image

Just trying to add a button to a navigation bar from an image.
code:
UIBarButtonItem *newConvoButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"convos_new.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(newConvoInit:)];
self.navigationItem.rightBarButtonItem = newConvoButton;
result:
(It should be just the dark image without the blue button in the background.)
This is likely overkill for what you want. But I have a good feeling that this will make your life a whole lot easier. The following will give you just an image without any UIBarButtonItem attributes.
UIImage *menuImage = [UIImage imageNamed:#"navBarMenuButton.png"];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, menuImage.size.width, menuImage.size.height);
[leftButton setBackgroundImage:menuImage forState:UIControlStateNormal];
aController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
This method implements a custom UIButton, which given the frame of the UIImage you're using, will give you nothing else but the image of your choice added to your UINavigationBar.
A bonus is that you don't have to worry about re-sizing anything in case the image ever changes in the future because the frame inherits from the UIImage.
Best of luck!

UIBarButtonItem not centered

I've added a single button to the center of a toolbar using:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:nil];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
self.toolbarItems = #[spacer, addButton, spacer];
But the added button is not quite centered:
What is going on here?
Don't add the same spacer twice in the array - the value may be different in each - make two spacers.
I'm starting to think it's some sort of an iOS-wide bug. That, or they got it wrong in many places. For example, paging dots on home page aren't centered. The Notification Center's top 3 buttons (Today, All, Missed) are also a few pixels off. Plenty more like that. So this may be corrected in the next update.
For now, I'd try to force it to center (say, in viewWillAppear). Pseudo-code below, no time to test :P:
self.toolbarItems[1].center = CGPointMake(self.bounds.size.width / 2.0f, self.bounds.size.height / 2.0f);

Resources