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

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.

Related

UIButton appearance setBackgroundColor effecting navigation bar now after update

Xcode or iOS update has made the following code below function differently because now the navigation bar button background has a background color, unlike previously. Any fix? I want all buttons to have the same global color, but now it effects the navigation bar buttons which I dont want. I would like it transparent like before.
[[UIButton appearance] setBackgroundColor:[Helper getColor:self.application.color]];
As this particular UIButton is inside a UINavigationBar you could try applying a second specific appearance to 'override' the first general appearance set.
For example calling specifically,
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundColor:[UIColor clearColor] forState:state barMetrics:metrics];
after you call,
[[UIButton appearance] setBackgroundColor:[Helper getColor:self.application.color]];
To override the general with the specific.
Docs Link: https://developer.apple.com/documentation/uikit/uiappearance

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

Cant set rightBarButtonItem tintColor

Im attempting to set only the rightBarButtonItem of my navigationItem's on navigationBar. I'm using this code:
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
The second line does nothing however and both the left and right UIBarButtonItem stay red. What could be causing this issue?
I have also tried:
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
[self.rightBarButtonReferenceIBOutlet setTintColor:[UIColor whiteColor]];
I have also tried just using this line of code:
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor redColor]];
And the right bar button remains red. I cannot figure out what is causing this issue.
PS.. still no answer Set color of one UIBarButtonItem using IBOutlet
Also seems the same issue is here UIBarButtonItem setTintColor doesn't work on iOS7

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.

Remove UIBarButtonItem image

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).

Resources