I am developing an application in which there are 7 buttons in every view.
We can switch on any view with respective to button clicked.
Now I want to enable(by change color or background or any thing) the clicked button, means all buttons should remain as it is only the clicked button should look different.
you can use
[button setImage: forState:];
This will help if u are using images for the button
[button setBackgroundImage: forState:];
You have two images like "selectedImg.png" and "unselectedImg.png" and change them according to the selection.To change the image use the following setter method,
[selectedButton setBackgroundImage:selectedImg.png forState:(UIControlState)state]
In the case of "setImage:" the image size will not resized for your button size.But in "setBackgroundImage:" will resize and set that as background.
Related
I have multiple buttons in an outlet collection with no image defined on any of them in the inspector. I use a for loop and the following line of code to set the image on each button after each interaction.
[button setImage:(UIImage *)image forState:(UIControlState)UIControlStateNormal];
When the application starts in the simulator, some of the buttons have the image neatly fit in the button, the others have a tall, skinny version of the image. If I understand how this works, at the beginning the code to set the image has not been executed.
How is the image being displayed at the beginning?
How do some buttons have the correct image and others are screwy?
It sounds like your buttons may not be all using the same contentMode. Look at the view in interface builder and go to the attributes inspector and under "View" you should see a "Mode" drop down with values like "Scale to fill", "Aspect Fit", "Aspect Fill". You should make sure this value is the same for all your buttons. (Obviously use the value from whichever buttons are displaying the way you want). You can see what each mode option does here:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Use this if you don't have a background.
[button setBackgroundImage:image forState:UIControlStateNormal];
The background image is scaled to fill the bounds, but the image is not.
I have created in the XIB a custom UIButton with no image. When the button is clicked, the title disappears.
These answers didn't help me because, I do not have any image that can overlap the text
After push a button it's title disappears
iOS - Interface Builder: UIButton title disappears when setting image
UIButton with custom background loses title
This didn't help either, because I do not have any background color
UIButton title disappears
This one didn't help too
Button changes title every time pressed
UIButton and its subclasses have 4 states which can change by code or stage config in IB
normal
highlighted
selected
disabled
Check UIButton.h in UIKit framework and you'll see how to use them:
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
If you setTitle, image, or backGroundImage for normal state, the default one of other states are same as normal one.
Default state is Normal, other state can be set:
[button setHighlighted:YES];
[button setSelected:YES];
[button setEnable:NO];//disable state
Button change from Normal to Highlighted on click, so if you want to keep normal title, please check:
-Don't use setImage or config image for normal and hightlighted state(just use backgroundImage)
-Don't setTitle:#"" forState:UIControlStateHighlighted or config to
nothing in IB.
-Don't setTitleColor or choose titleColor in IB same as UIButton backgroundColor.
You can test with 4 different titles and backGroundImages for 1 button and know how can it display.
In an XIB, you can set the title for each of the different states by changing the State Config and then setting the title for the corresponding state. In the following example, I have set the Default state title to Title and the Highlighted state title to a single space, which makes it appear as blank in the app. Note however, that the preview only shows the Default settings, and does not update for the different configurations (see images below).
Default State Title
Highlighted State Title
I have had this happen as well. What I believe the issue is, is since the button is default a white rounded button with the default "highlight" state of a button, and inverts the text color as the button is pressed, it does the same with a custom button. Now, since the text is a blue, and the background is white for a rounded button, it is quite the same in a custom button. The custom button's text will be whatever you want(set) it to be. The background is usually a transparent color as well, and since when the custom button is highlighted, it causes the text color to be inverted, just as a regular button does. This causes the textcolor to become transparent, and the background of the custom button just doesn't change because there is nothing to change to, hence the fact that the button is custom, (meaning you must set the highlight states and other attributes of the button). I have had this happen before, and I never really realized this before, but I hope this helps you out!
// for setting text in normal state
[_myButton setTitle:#"myText" forState:UIControlStateNormal];
// show another text on touch
[_myButton setTitle:#"myText" forState:UIControlStateHighlighted];
I've customized UIBarButtonItem in my AppDelegate.m using UIAppearance and all the bar buttons are showing the correct images in normal and selected states.
I want "Done" and "Save" buttons to use a different image (so they appear to be blue instead of gray), so I use -setBackgroundImage: forState: barMetrics to set two different images for UIControlStateNormal and UIControlStateSelected in -viewDidLoad.
The image for UIControlStateNormal is working fine, but the UIControlStateSelected one isn't showing, instead it shows the original image I used for selected states in AppDelegate.m. If I comment out the original image the new (blue) one is shown properly.
What's the proper way to set different background images for specific buttons?
I believe UIControlStateHighlighted is what you are looking for.
A UIBarButtonItem goes into the highlighted state whenever it receives the user touch. UIControlStateSelected is typically for UI elements which toggle between two states.
I have a tableview witch display video thumbnails. I would like to add/overlay a "play" icon on top of the thumbnails.
How to add this icon on all my thumbnails?
Add a custom button over your video thumbnail image with a transparent play png. Doing this will also allow you to hook that custom play button up to an action that could allow the user to play the video.
Here's what I do for my app:
UIImage *play = [UIImage imageNamed:#"play1.png"];
self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.playButton setImage:play forState:UIControlStateNormal];
[self.playButton addTarget:self
action:#selector(playVideo)
forControlEvents:UIControlEventTouchUpInside];
Something that drove me nuts: Be sure to use a png NOT named "play.png"!
Agree withWrightsCS. You may want to seal your thumbnail into a separate class (let's say, a new subclass of UIView). And you customize it and put an transparent play icon on top of it.
Then you go back to you tableview's cell's class, and hook up that new thumbnail class with an action you want it to trigger.
Programmatically I am creating buttons and keeping some background image using setBackgroundImage. I want to remove the set background image in the button after some time interval. I have timer code, which will call after particular interval. At this time, i want to remove the button background image in my program. I'm done with timer code and all. I don't know how to remove button background image programmatically. How to do that removing background image from buttons? Is it possible to remove the already set background image for buttons? Can i make it null as setBackgroundImage again? I can't put white color back for button so as to like removing, because my application screen has some other color design.
Appreciate your help on this !
thanks.
Just use [button setBackgroundImage:nil forState:UIControlStateNormal]. However, this will give you a pretty much transparent button (except for the button title). Are you trying to hide the entire button? If so, use button.hidden = YES, or button.alpha = 0.0.
You need to hang on to a reference to your button, but as long as you have that, you should be able to call [button setBackgroundImage:nil forState:UIControlStateNormal] from your timer.