How do you set the background image of a plain UIBarButtonItem? - ios

If my UIBarButtonItem (lightButton) has a style of Bordered, either of the following lines change the background image:
[lightButton setBackgroundImage:resizeableImage forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault];
[lightButton setBackgroundImage:resizeableImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
If the button has a style of Plain, then neither of the following lines change the background image:
[lightButton setBackgroundImage:resizeableImage forState:UIControlStateNormal style:UIBarButtonItemStylePlain barMetrics:UIBarMetricsDefault];
[lightButton setBackgroundImage:resizeableImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Is it possible to change the background image of a Plain UIBarButtonItem, other than using initWithCustomView:?

you can use it.
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* imagex = [UIImage imageNamed:#"xxxx.png"];
[modalViewButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
[modalViewButton setImage:btnImage forState:UIControlStateNormal];
//modalViewButton setBackImage......
[modalViewButton setFrame:CGRectMake(0, 0, imagex.size.width, imagex.size.height)];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];

Related

UIBarButtomItem image highlighted

I'm trying to setup a different image (highlighted) when the user press the UIBarButtomItem with this code:
self.addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"addButton"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(addAlert:)];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"addButtonHigh"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = self.addButton;
But it is not working.
The button appears with the "addButton" image, but when it is pressed the "addButtonHigh" image doesn't appear.
Thank you in advance,
Victor
change UIControlState from UIControlStateSelected to UIControlStateHighlighted. If you want to change the background image highlighted. You need to change the UIControlState.
The following is the code snippet i test. it works.
self.addButton = [[UIBarButtonItem alloc] initWithTitle:#"hello" style:UIBarButtonItemStylePlain target:self action:#selector(addAlert:)];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"font_minus_32.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"font_plus_32.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = self.addButton;
Also maybe the following code is the code you wanted.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"font_minus_32.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"font_plus_32.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(addAlert:) forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
self.addButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = self.addButton;
- (IBAction)buttonClicked:(id)sender
{
UIImage *buttonImage = [UIImage imageNamed:#"home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
}
UIControlStateHighlighted Highlighted state of a control. A control enters this state when a touch enters and exits during tracking and when there is a touch up event. You can retrieve and set this value through the highlighted property.
UIControlStateSelected Selected state of a control. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, the UISegmentedControl class) may have different appearance depending on their selected state. You can retrieve and set this value through the selected property.

Can't change the background for UIBarButtonItem in a separate View Controller

in my appDelegate:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[UIBarButtonItem appearance] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
and in separate view controller:
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:nil];
[backButton setBackgroundImage:[UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem setBackBarButtonItem:backButton];
So on the child view controllers the title of the back button is changed but the background is taken from the appearance in appDelegate. I need a custom background for one view controller. How can I solve this?
edit#1:
now appDelegate looks like this:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[SettingsListViewController class], nil] setBackButtonBackgroundImage: [UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
You can achieve this by adding constraint to the appearence as follows:
[UIBarButtonItem appearanceWhenContainedIn:[YourOtherVC class], nil] setBackButtonBackgroundImage:navBarArrowItemBackground
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Your code should be look like as follows:
UIImage *navBarArrowItemBackground = [UIImage imageNamed:#"tabbar_button_back"];
[[UIBarButtonItem appearanceWhenContainedIn:[SettingsListViewController class], nil] setBackButtonBackgroundImage: [UIImage imageNamed:#"tabbar_button_back_gray"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UIBarButtonItem appearance how to remove text?

I am customizing my nag bar button in iOS7 and I am using the following code:
// Customizing the NavBar Buttons
UIImage * btHome = [[UIImage imageNamed:#"Home.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 3, 0, 3)];
[[UIBarButtonItem appearance] setBackgroundImage:btHome forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It is working, but the view shows my home button with the button text over it, I do not want the text to show.
What else do I need to do to remove the text?
What I have is shown here:
Try to set
1) text color on a bar button item through appearance
2) use setImage: instead of setBackgroundImage: and do not use resizable image
Try with this code. May be it will help you.
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *yourbutton = [UIButton buttonWithType:UIButtonTypeCustom];
yourbutton.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[yourbutton addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:yourbutton];
self.navigationItem.leftBarButtonItem = backButton;
Sets the back button title offset for given bar metrics
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

Customizing UIBarButton makes it too big

So when I try to set a custom color to a UIBarButton, it causes the displayed button to be too big and it extends down past the NavigationBar and into the main view of the app. Is there an easy way to fix this? Here is my code:
UIImage *buttonColor = [[UIImage imageNamed:#"blue-background.jpg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonColor forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
try playing with the values of the UIEdgeInsetsMake function.
in one of my apps it looks like this;
UIImage *backbuttonimage = [[UIImage imageNamed:#"backbutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backbuttonimage
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
it made it as i wanted... if you have any problems i would like you to tell me and if you can add a photo of how it looks for me to understand better the problem.
these are my values for my app:
UIEdgeInsetsMake(0, 15, 0, 10)
have a nice day :)
This is how i make custom UIBarButtonItem and it works perfect for me. Try this:
UIButton *postView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 61, 30)];
[postView setTitle:#"Post" forState:UIControlStateNormal];
[postView addTarget:self action:#selector(showAddPost) forControlEvents:UIControlEventTouchUpInside];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateNormal];
[postView setBackgroundImage:[UIImage imageNamed:#"bbi_texture.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *postButton = [[UIBarButtonItem alloc] initWithCustomView:postView];
[self.navigationItem setRightBarButtonItem:postButton];

How to set UIBarButtonItem with out border

I have created view with barbutton as follows
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settingsicon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonClicked:)];
It is comming like this
but I want it as
how can I remove the border of the button?
Use this :
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Hope it helps you.
Another way is to remove button's background image:
// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];

Resources