This question already has answers here:
Is it possible to change a UIButtons background color?
(17 answers)
Closed 8 years ago.
I am drawing custom a UIButton and want to set the blue color when it is highlighted. How can I achieve this?
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:[[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:0] forState:UIControlStateNormal];
You can do following to set images for your state (normal/highlight/selected). You have to have images.
[downButton setImage:[UIImage imageNamed:#"white.png"] forState:UIControlStateNormal];
[downButton setImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateHighlighted];
Ya, you can create a 1px by 1px "blue colored" image and use the following:
[yourButton setBackgroundImage:[UIImage imageNamed:#"theImageYouMade.png"] forState:UIControlStateHighlighted];
You can't use "setImage:" in my method because the highlighted state of the button will actually just display the 1px by 1px image in the center of the button.
My method works for variable sized buttons. Using setImage: requires you to make images that are exactly the same size as your button.
-Chris
You can try to use another image to do the job. You can also refer to this link:
Is it even possible to change a UIButtons background color?
Maybe set a different background image for a different state?
Provide a background image for UIControlStateHighlighted. Also it might help to turn off adjustsImageWhenHighlighted, though this should not matter if you're providing a separate background image for the highlighted state.
Related
How is it possible to have something like http://www.nutsaboutmac.com/wp-content/uploads/2012/11/ShareLinktoFacebook.png
Until now, I've put the same background image for my NavigationBar and my BarButtonItem. The problem is that the button is no longer bordered, you can't do the difference between the NavigationBar and the Button anymore...
UIImage *image = [UIImage imageNamed:#"facebook_texture.png"];
[cancelButton setBackgroundImage:image forState:UIControlStateNormal style:UIBarButtonItemStyleBordered barMetrics:UIBarMetricsDefault];
[topBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
How can I change the background but keep the bordered style ?
you can't set a custom bg image. the border is the bg image and it is either your own or the system.
=> no way but making your own image
Edit:
I THOUGHT for what you describe use tinting: barButton.tintColor=color and supply a color either with an alpha or a pattern image made with colorwithPatternImage
but... the tinting with a clear color or a pattern doesn't work. this is a bug in the sdk IMO
=> no way but making your own image
i have that image background with transparent part and i went using for my buttons without
transparent part and if possible with interface builder
By the sounds of it you are possibly using the UIButtonTypeRoundedRect style, but UIButtonTypeCustom may be more suitable. You can change this within Interface Builder's inspector window.
still i refer this link http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
btnImage = [UIImage imageNamed:#"image.png"];
[btn setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn setTitle:#"Title" forState:UIControlStateNormal];
or you can directly set background image from your xib by changing it into custom type.
Hi I have a couple of images that are covered by an invisible button. Is there a way that I can make it look like the images were pressed down when I press the invisible button? Thanks!
You can put the image directly onto the button, you dont need a seperate UIImageView.
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateSelected];
The are a few more UIControlState constants.
Take a look at the docs:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
Im trying to troubleshoot my first iphone app and Ive run into a propblem i just dont get. I have lots of buttons on a view and when you click them their text colour changes from black to blue. I assume that I could fix this by just setting the textColor property every time the button gets pushed to set it back to black but I feel like there must be something else going on.
Anyone know why my buttons are changing color by themselves?
It is possible to have different title colors depending on button state. Set them using the following method of the UIButton class:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
F.e.
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
where UIControlStateNormal is the default, not pressed, button state, and forState:UIControlStateHighlighted represents the pressed state. But there are more, and can be combined with binary sum, such as (UIControlStateHighlighted | UIControlStateSelected).
I have an UIButton in my application.
I want to show a selection of my button. I am using this code:
[Button setHighlighted:YES];
But it's not working.
Can anyone help me?
You can solve this problem by having two images for button. One for highlighted state and one for normal state. When you create the button, create with normal state image as buttons background image. And when you select or focus on the button, change the image as highlighted background image.
[Button setBackgroundImage:[UIImage backgroundImage:#"ButtonSelected.png"] forState:UIControlStateSelected];
[Button setBackgroundImage:[UIImage backgroundImage:#"ButtonNormal.png"] forState:UIControlStateNoraml]
See these posts.
http://forums.macrumors.com/showthread.php?t=530998
http://simonwoodside.com/weblog/2009/2/5/how_to_make_an_iphone/