Remove UIBarButtonItem image - ios

I have a UIBarButtonItem that I originally change the background image to. But I would like to be able to change the same UIBarButtonItem back to a default looking one (specifically a done button). Then again back to the way it was prior to (that shouldn't be a problem).
Here is how I change the appearance at first:
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Then back I was trying things like:
menuButton.style = UIBarButtonItemStyleDone;
menuButton.title = #"Done";
//the above didn't do anything, so I tried to make my own image to
//replace the first image. But below did't do anything either.
[menuButton setBackButtonBackgroundImage:[UIImage imageNamed:#"doneButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Use setBackgroundImage:... instead of setBackButtonBackgroundImage:..., you're only setting the appearance of back buttons (in navigation bars).

Related

iOS right bar button background image overlap with title on ios11

I am facing issue in margin of right bar button item title.
From iOS11 background image is overlapped means it is not properly right aligned.
It was working pretty good till iOS 10.
Code snippet:
UIBarButtonItem *rightItem = self.navigationItem.rightBarButtonItem;
[rightItem setBackgroundImage:[UIImage imageNamed:IMAGE_NAVIGATION_NEXT_BUTTON_DISABLED_iOS7]
forState:UIControlStateDisabled
barMetrics:UIBarMetricsDefault];
[rightItem setBackgroundImage:[UIImage imageNamed:IMAGE_NAVIGATION_NEXT_BUTTON_iOS7]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
rightItem.title = #"Next";
Before iOS11 it was working like this

iOS 7+ UISearchBar cancel button has unwanted alpha on highlighted state

I have UISearchBar with cancel button. Cancel button has custom backgroundImage for normal and highlighted states sets in appDelegate:
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundImage:[<someNormalImage> resizableImageWithCapInsets:<someedges>] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundImage:[<someHighlightedImage> resizableImageWithCapInsets:<someedges>] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Everything works fine. Images are showing, except of highlighted state, where backgroundImage is showed with unwanted alpha (opacity).
When button is highlighted, highlighted background blink and then opacity is changed.
I wasn't able to find any answers to this problem, only how to change appearance of cancel button (which is working well).
Thanks for any help.

UINavigationBar Back button duplicates twice

I have used the following code in my appDelegate and customizing back bar button goes well. When i used segue for the controllers navigation backbarbutton appears twice.
UIImage *buttonPortait = [[UIImage imageNamed:#"back-icon"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0,0, 0,0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonPortait
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
I also used navigationBar setTranslucent:NO but yet the problem surviving. Any solution for this problem. Thanks in advance.
I got the solution by altering the image size for the back bar button.
I used image size with height 30x(non-retina)/60x(retina) and width 90x(non-retina)/180x(retina)
cheers!!!

Back button strangely disappearing in UINavigationController but keeps working

Under iOS7 I've been experiencing an issue where the back button item will not show up if it has been set with a specific background image:
int imageSize = 21; //REPLACE WITH YOUR IMAGE WIDTH
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
forBarMetrics:UIBarMetricsDefault];
UIImage *barBackBtnImg = [[UIImage imageNamed:#"BackArrowDark.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Upon doing this, any ViewController that I push in the navigation controller will have no back button item appearing, even though pressing where it should be, will make it appear, and any subsequent pushes of this view controller will have the button present on the screen.
This issue is only appearing under iOS7: everything works perfectly under iOS6.
Changing the back button completely with a leftBarButtonItem disables the back swipe, so that is not an option.
Any idea what I am doing wrong?
Thanks much for your consideration.
After trying different solutions, I found that changing the backIndicatorImage works best under iOS7, and it seems to be in line with the iOS7 interface paradigm:
[[UINavigationBar appearance] setTintColor:[UIColor grayColor]];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; // Takes out title
UIImage *backButtonImage = [UIImage imageNamed:#"BackArrowDark.png"];
if ([UINavigationBar instancesRespondToSelector:#selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
} else {
int imageSize = 21; // REPLACE WITH YOUR IMAGE WIDTH
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
With this method:
When going back in the navigation controller, the back button item transition is the same as with the default indicator (a departure from the back button sliding away as well under iOS6);
Under iOS6, the backButton is changed and keeps its default iOS6 behaviour.
I'm happy!
Make sure you are not calling this in the view controller:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
In iOS 7, this will tint the navigation bar but will also make your buttons invisible, yet functional just as you are describing.

iOS Can't change back button image

I'm trying to change the back button image using a resizable image of just a 1x1 pixel (I just want to change the color of it basically and remove the tint). The code I'm using is the following:
UIImage *backButtonImage = [[UIImage imageNamed:#"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But for some reason, the button seems like it is only allowing a 1 pixel line to be show:
You can see a sliver of the text of the title of the previous view.
Is there any way to just change the color of the back button and remove the tint so it's a solid color without having to create a new barButtonItem and remove the one already there?

Resources