I have assigned a button (holdCardOne) an image in the interface builder. What I want to do is change the image when a card is selected from a picker. I have the picker working and selecting the card but I need to change the image on the original selected button to resemble the selection.
Can I do this using code?
Does it matter I assigned the image in the interface builder or does it have to be all done by code?
You should be able to change the button image using the setImage:forState: method of the UIButton class. See the UIButton class reference for more info.
In terms of using the Interface Builder, it would be safe to set the initial image in that, but you'll need to handle things yourself from that point onwards. (e.g.: If a user can un-associate an image with a button, you'll need to set an appropriate image programatically as above.)
You can do it either in code or in the interface builder or both!
InterfaceBuilder lets you assign images for each button state (normal, highlighted, selected) -- so it could be as easy as assigning images for each state, and then simply changing the state in your code.
Alternatively, you can set the image directly, simply by saying:
[myButton setImage: (someImage) forState: UIControlStateNormal];
You might want to also set this image for the other buttons states (highlighted, selected), it all depends on how you want the interface to behave.
It doesn't matter that it was assigned in Interface Builder. All you need to do is this:
[yourButtonName setImage:yourNewImageName forState:UIControlStateNormal];
You'll also likely want to set an image for
UIControlStateHighlighted
and possibly for
UIControlStateDisabled
and
UIControlStateSelected
Related
I have many controls like, image views, labels (UIControls), which have I wish to show like a dual mode controls. i.e. Based on my data, I have to set them either with image 1 or image 2 (for a image view), similarly with the label. I tried accomplishing this using the highlighted state properties of image view and labels. For the image view, I gave one image reference for highlighted and another for normal.
however when I programmatically set the highlighted property to yes, they are not toggling between them. Is there something I'm missing?
From the documentation:
Highlighted state of a control. A control enters this state when a
touch enters and exits during tracking and when there is a touch up
event. You can retrieve and set this value through the highlighted
property.
So, you don't set the highlighted property. Try setSelected.
Have you seen this related topic: Highlighting a UIControl subclass?
Use selected state instead. I think highlighted state is a transient state.
Try this out:
if([imgeview isselected]){
[imageview setselected:NO];
}else{
[imageview setselected:YES];
}
I'm developing an iOS 4 application.
I'm using a custom uibutton to make an image clickable. When user taps over the image, it will disappear.
It's not very pretty to see that the image gets black, then turns to its original color, and then disappear.
Is there a way to disable that effect?
You will need to set the property adjustsImageWhenHighlighted to NO:
[button setAdjustsImageWhenHighlighted:NO];
Alternatively you can set the same image for all controlStates of the button.
You need to set the property Shows Touch On Highlight to enabled.
Programmatically you can do that with:
[button setShowsTouchWhenHighlighted:YES];
Since it's a custom button, you get to specify the image you want to show when it's highlighted. Create the image you want to show in that situation.
If you are using interface builder just set the Highlighted and selected states to the same image as the default image.
I have a button connected to an action.
When I set the background image on that button, the result is not visually appealing. I want just to see the image not the button but I want the ibaction functionality of that button.
Please help
You need a button with type UIButtonTypeCustom (this will stop the default button appearance from drawing). You can do this in interface builder or in code (although you can't change it once the button is initialized).
i have a uibutton. im using two images for UIControlStateNormal and UIControlStateSelected. when i touch down the button it shows the UIControlStateSelected image for a split second. what is the reason?
I'm not sure i've got ur question correctly but try setting the image for UIControlStateHighlighted instead of UIControlStateSelected if you want the other image to be displayed as long as you keep the button pressed.
Reference: UIControl Documentiation says,
Note that the control can be in more than one state
In you case Selected and Highlighted. So UIControlStateNormal image is the default image used for highlighted state.
This is my best guess.
Good Luck,
Swapnil
I want to create a UIPickerView with the easiest way possible, because all of what I know Is not enough to help me
Ok what I ment is that I am trying to get a UISegmented that will end up creating a UIPickerView but I want to create a UIPickerView that shows up when I select one of the segments in the segmented control animated, and also the pickerview is supposed to use an array that has colors like red green blue
Ok what I ment is that I am trying to get a UISegmented that will end up creating a UIPickerView but I want to create a UIPickerView that shows up when I select one of the segments in the segmented control animated, and also the pickerview is supposed to use an array that has colors like red green blue
For your UISegmentedControl, you'll need to register target-action methods. For example:
[segmentedControl addTarget:self
action:#selector(updatePicker:)
forControlEvents:UIControlEventValueChanged];
Inside of updatePicker, set a variable that corresponds to whatever you want to show in your picker. Then, to your picker instance (you should only have one instance of this class), send the reloadAllComponents message. This will update the picker by querying the delegate for new data. In your delegate (which can probably all be the same class), you'll want to check the value of the variable you set in updatePicker to determine what data to return to the picker.
This page has video and instructions on how to add UIPickerView programmatically in Swift, i.e. it does not use Interface Builder or Story Board, everything is done in a few lines of code.
http://www.appswiftprogramming.com/pg/2171/UIPickerView_Programmatically
You can add your custom view in the UIPickerView if you want something more advanced. It shows that too.