iPhone:How to remove button background image programmatically? - iphone

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.

Related

How to create a gray overlay for a UIButton in touched state programmatically?

I would like to make a generic class that when tapped, makes the element grayish.
Facebook's app is the perfect example of what I want to achieve. All their links and images become gray when tapped.
I can only guess that they are subclassing UIButton.
I have made my button's style UIButtonTypeCustom to get rid of the rounded border. Beyond this, I don't know how to have the gray overlay because I see no such property in the documentation.
Its simple:
#define TAG_GRAYVIEW 5671263 // some random number
// add the gray overlay
UIView *grayView = [[UIView alloc] initWithFrame:button.bounds];
grayView.backgroundColor = [UIColor grayColor];
grayView.tag = TAG_GRAYVIEW;
[button addSubview:grayView];
// remove the gray overlay
UIView *grayView = [button viewWithTag:TAG_GRAYVIEW];
[grayView removeFromSuperview];
I think you need to use a semi transperant grey image PNG file. You need to then set Image of button in Highlighted state.
Also note that both the images for Normal State and Highlighted State need to have the images with titles on them.
As once we set the image to button, btn.titleLabel.text won't be displayed.
So you can have a image with transperant background and title on it for Normal state. And an grey image with title on it for Highlighted State.
Code for doing it programmatically is
[btn setImage:#"Transperant.png" forState:UIControlStateNormal];
[btn setImage:#"Grey.png" forState:UIControlStateHighlighted];
Hope this helps you.
The default UIButton masks the opaque content using a gray highlight. For example when using a transparent png, only the parts that contain non-transparent pixels will be grayed out on touch. The default UIButton highlight has no effect on subviews/sublayers, and will only work on provided images. What you see in the Facebook app is probably just a UIWebView highlight, possibly customized using css.
To create something similar using your own control (to prevent the overhead of a UIWebView) you should probably create your own UIControl subclass and highlight the content on touch using a transparent CALayer. You can even mask the CALayer to only highlight the actual contents, instead of a rectangular shape.
Also see my stackoverflow post on creating custom controls for iOS by subclassing UIControl.
Try setting the button up something like this.
*mind you I didn't create the image for this example.
Set your button type to custom, and in "State Config" select "Highlighted" from there you will want to set the image of the button to be a semi-transparent grey image. There are probably several other ways to achieve this same effect but this one should be nice and simple.

UIButton with changing images when pressed - but unattractive effect when changing them

I have a button that changes its image to different images depending on what modus we are in.
The images are set with:[modusBtn setImage:cx forState:UIControlStateNormal];
The images have round corners and the button is of Type "Custom".
Now when the button is pressed one can see another image in the upper and lower edge background.
Really weird - the buttons ALL have round corners - but for example when the blue modus is on and shows the blue image and one presses the blue button now - one can see red little edges only on the left two corners.
I already played around with all the button attribute settings in IB, but no luck.
Did anyone have a similar experience when changing button images?
So far all my button images where 100% rectangular and did not have round corners - therefore I never experienced this before.
Many thanks!
Use this UIButton's propriety for your button.
avatarButton.clipsToBounds = YES;
Whenever you set an image for custom button make sure to set
yourButton.backgroundColor=[UIColor clearColor];
The effect was happening as your button was using default background transitions during changing the state.

iPhone: How to avoid background color coming up when button is selected?

I have a button with a curved image set as its foreground image. It looks like:
Now when the button is on pressed state, it shows up like this:
notice the grey rectangular color coming up around the edges. Is there any easy way to avoid the grey background coming all over the rectangle ?
You need to set the property Shows Touch On Highlight to enabled.
Programmatically you can do that with:
[button setShowsTouchWhenHighlighted:YES];

how to show active button in iphone app

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.

can i add progress into a UISlider?

This picture shows what i want , i just making a custom movieplay controller.
alt text http://www.imagechicken.com/uploads/1279252703012036800.png
I know how to make a custom uislider as code below
playSlider.backgroundColor=[UIColor clearColor];
[playSlider setThumbImage:[UIImage imageNamed:#"sliderButton.png"] forState:UIControlStateNormal];
[playSlider setMaximumTrackImage:[UIImage imageNamed:#"sliderGray.png"] forState:UIControlStateNormal];
[playSlider setMinimumTrackImage:[UIImage imageNamed:#"sliderBlue.png"] forState:UIControlStateNormal];
but that can not help me .Is there any ideas? Thank you for any advices!
I think you could get away with a UISlider only if the thumb was never allowed to leave the blue (loaded) area. Then you could draw the inactive (unloaded) track to the right of the actual slider bounds, and just update the bounds for the slider as the video loads, which would increase the blue area. But you'd still have to do the right math for the slider thumb's relative progress.
If you need the thumb to swipe into the unloaded area, or you want to avoid a headache with the recalculation, you should just build your own control. Unfortunately, the UISlider is not a highly customizable control by itself.
You should be able to subclass drawRect on the UISlider to customize that, but I may be wrong.
overriding drawRect: is not a good idea.
You will have to make a custom object containing something like:
an UIButton for the play/pause, an UISlider for the slider, an UIButtons for the mute on/off,
and a UIView for the volume (you will have to implement by your self)
Actually is not difficult, is just an container object.