uiappearance for UIButton also changes UIBarButton image? - ios

I am using this piece of code
[[UIButton appearance] setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
To change the appearance of UIButtons. This works nicely and does not affect UIBarButtonItems except for one of them (The only one that is not an auto generated "back" button) and only initially. When you touch the barbutton, it reverts back to not having a background image.
The problematic button is a UIBarButton and not an UIButton (Unless it is both, in which case the other UIBarButtons should be affected too).
A clue can be that the other UIBarButton that are not affected are all generated and not created by me in the storyboard.
This must be a bug in the api?

Solved using setTintColor. Was an api bug that since then probably has been fixed

Related

UIButton Programmatically get Default Highlighted Style

I searched for this problem for a while and couldn't find anything about it, but eventually figured out my mistake, so I figured I'd post it here to help anyone with the same situation.
When creating UIButtons in Interface Builder, it automatically styles them for the highlight state (If you press and hold the button, it looks dim/lighter in whatever color you set it as); however, I was trying to create a button programmatically (just a normal text button) and wasn't achieving this result. If I pressed and held the button, the appearance was unchanged, although the button still worked.
The issue was that I was initializing the button with
UIButton *myButton = [[UIButton alloc]init];
instead of
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeSystem];
Which is apparently the best-practice way of initializing a UIButton.

Why my Bar Button item isn't getting hidden?

I'm using UIBarButtonItem in my project. I've tried to hide the UIBarButtonItem in iOS 6.1, but I was unable to do the same using the following code:
barbuttonname.tintColor = [UIColor clearColor];
barbuttonname.enabled = NO;
This code hides the UIBarButtonItem in iOS 7.1, but in iOS 6.0 it shows the UIBarButtonItem. How can this issue be fixed?
The reason is because use of tintColor for that purpose only became available in iOS 7. in iOS 6, the buttons also typically have borders and backgrounds and each bit is handled separately. In terms of what you're actually trying to accomplish here, I think you should go about it a different way.
Instead of modifying properties on the button to hide it, simply remove it from the navigation bar or wherever you have it. For example, if it is the right button on a UINavigationBar, you would just do:
myNavigationBar.rightBarButtonItem = nil;
Then, when you want to show it again
myNavigationBar.rightBarButtonItem = myButtonItem;

UIButton Highlighting Overcome default behaviour

In a program much like the who want to be a millionaire... I have 4 option buttons. It should flash red or green on press. The four buttons have either red or green images as background images. In the code setup I have such a method ready to setup the first question and when the second question gets set, the background images change accordingly.
Currently the default blue highlight is always appearing on the first pressing of any one the buttons and after that there are other issues.
I could paste some different approaches here, (but they dont work). So any suggestions would be good.
Also in the IB, when you set the highlight with background image, that works. I would use use, but for the same button, the back ground image must be changed programmatically.
Thanks in advance for the help/suggestions!
Use two State UIControlStateNormal and UIControlStateHighlighted
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btnMenu setBackgroundImage:[UIImage imageNamed:#"img1-Highlighted.png"] forState:UIControlStateHighlighted];

Changing appearance of UIBarButtonItem also changes position of navigationbar and toolbar?

once again I have a problem with my custom navigation- and toolbar:
I've customised their tintColor and the font by using the appearance proxy in didFinishLaunchingWithOptions. Initially the bars should be hidden by setting their center outside the visible area in viewDidLoad. By single-tapping I use my own animation to slide the bars in/out. Everything was working just fine until the next step:
I wanted the UIBarButtonItems to have the same font like i used in the bars, so I went back to didFinishLaunchingWithOptions and added the following code:
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_HEADER size:0.0],UITextAttributeFont,nil] forState:UIControlStateNormal];
It works just fine for the font part, but somehow it also sets the bars back into the visible position before viewWillAppear so the bars are not initially hidden anymore. I tried to find out what causes this unmeant repositioning and couldn't find any connection. I also tried to reset the position at some later point like viewWillAppear but this somehow doesn't work for the toolbar.
EDIT: The described behaviour does only occur on devices with iOS 5 though. On the iOS6 simulator everything still seems fine.
Does anyone have a hint for me what is going wrong here or how I could smoothly solve this problem?
Thanks in advance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_HEADER size:0.0],UITextAttributeFont,nil] forState:UIControlStateNormal];
what's the FONT_HEADER here? Did you check if the ios 5 support this font?

UIButton with cust. background looks bad when pressed

I have a UIButton with a custom background that I set in the Interface Builder (see picture). It looks nice, but in the pressed state it gets really ugly (see the 2nd picture). What's going on here?
Normal:
Pressed:
[btn setBackgroundImage:[[UIImage imageNamed:#"button_red.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 20, 12)] forState:UIControlStateNormal];
[btn setBackgroundImage:[[UIImage imageNamed:#"button_red_pressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 20, 12)] forState:UIControlStateHighlighted];
This should solve the problem.
Also, change your button style to Custom, this will remove that blue button background.
Post some code or maybe the image itself. It looks as if you might need to declare your 'pressed' image with one of UIImage's resizableImage* methods. It's stretching to fill the button rect.
Also, I'm fond of the MOButton and MOGlassButton classes used in the iPhone SOCKS project (handy in itself).
Update
The following worked for me, it even animated it dark when pressed:

Resources