Question regarding UIButton - iphone

everyone
I have 1 UIButton with 1 image with blue colour
but when i click on UIButton at that time i want to remove that image and add 1 another image.

In code you can set button image following way:
[yourButton setImage:yourImage forState:UIControlStateHighlighted]; // or for whatever state you want
You can also set all necessary images in IB for all possible states (default, highlighted and selected)

Related

How to detect objects from Image in iOS? [duplicate]

This question already has an answer here:
How to detect objects from Image Processing in iOS?
(1 answer)
Closed 9 years ago.
I have an image, on an image there are three things Spoon, Cup and basket.
Now I have to identify when user click on image nothing will happen until and unless the user click on spoon, cup & basket.
If user click on spoon the spoon colour will become green.
Same for cup and basket.
And I have no idea how to do this. I want some hints from experts and how and where to start?
Any idea or suggestions would be highly welcome.
You can try out masking of views by setting the visible parts as the images of objects and set that view with the gestures
or
Make different images for the objects set it in the selected state of button and on button click change the state to selected
[button setSelected:YES];
[button setSelected:NO];
Place buttons on Spoon, Cup and basket. set button.backgroundcolor = [UIColor clearcolor]; Change images in IBAction.
lets say your image is in backgroundImageView. You got to keep 4 images. The original image, image with green spoon, image with green cup and image with green basket. And then add custom style buttons on top of spoon, cup and basket. And connect below IBAction methods to your buttons.
- (IBAction) spoonButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_SPOON.png"];
}
- (IBAction) basketButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_BASKET.png"];
}
- (IBAction) cupButtonPressed{
backgroundImageView.image = [UIImage imageNamed:#"IMAGE_WITH_GREEN_CUP.png"];
}
You can start investigating UIButton.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/
Make your images as Button background image in Interface builder or in code. You can do pretty much anything with image then.
Adding Buttons on your object. and go file inspector click on 5th tab make that button clear color your button dissapear.
then do what you want.
also tried to set image directly on buttons
[ButtonSpoon setImage:[UIImage imageNamed:#"spoon.png"] forState:UIControlStateNormal] ;
[ButtonCup setImage:[UIImage imageNamed:#"cup.png"] forState:UIControlStateNormal] ;
[ButtonBasket setImage:[UIImage imageNamed:#"basket.png"] forState:UIControlStateNormal] ;

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.

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.

how to set image in button

How set image in button when ever click the button image show me that button
run.backgroundColor = [UIColor redColor] I try this but it doesn't work,
To set an image for a button while it is currently being touched (I think this is what you're asking). You use setImage: forState on the UIButton.
[myButton setImage:[UIImage imageNamed:#"myButtonGraphic.png"] forState:UIControlStateHighlighted];
You can control when the image appears using the different control states, one of:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
UIControlStateApplication
UIControlStateReserved
If you use UIControlStateNormal, the image is displayed all the time, and when the button is currently being touched it will appear dimmer which for a lot of cases is good enough to indicate to the user that they are selecting that button and that it's a real touchable 'thing'.

How to highlight the button untill the next view is changed in iphone?

I have created five buttons in the view controller. If i clicked the button it goes to the corresponding view. Now i want to display the button in highlighted state when it is clicked. It should go back to the normal state only when i click the other button. I have set the another image for highigthting buttons when i clicked it, but it shows that highlighted state only one sec. Now i want to display the buttons highlighted till another button is clicked. Same like a Tabbar operations.(I have used buttons instead of tabbar for the requirements).
Now i have used the following code,
void didLoad
{
[btn1 setImage:[UIImage imageNamed:#"ContentColor.png"] forState:UIControlStateHighlighted];
[btn2 setImage:[UIImage imageNamed:#"bColor.png"] forState:UIControlStateHighlighted];
[btn3 setImage:[UIImage imageNamed:#"ShColor.png"] forState:UIControlStateHighlighted];
[btn4 setImage:[UIImage imageNamed:#"PicturesColor.png"] forState:UIControlStateHighlighted];
[btn5 setImage:[UIImage imageNamed:#"infoColor.png"] forState:UIControlStateHighlighted];
}
Please help me out.
Thanks.
You should be manipulating the image for selected state, not for highlighted state. Highlight will be unset on touch up or touch outside, as you have seen, whereas selected is persistent.
Simple solution would be...
on every click on any button change it's image (For normal state) to highlighted image for that particular button and set other 4 button's image to normal image...
It sounds like you should be using a UISegmentedControl with 5 segments instead of 5 buttons. It behaves this way already, a segment stays highlighted until you select a different one (unless you set the momentary property).