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];
}
Related
I am trying to set a state for UIButton.
But i don't know the difference between the UIControlStateHighlighted and UIControlStateSelected.
Could anyone help me out?
Thanks and best regards.
They can mean whatever you want them to, but in general they mean the following:
Highlighted = The user is currently interacting with something that will change once they stop interacting (e.g. holding down a button)
Selected = The item is current the active item in a group (e.g. The selected item in a segmented control). This can only be achieved by setting it programmatically.
UIControlStateHighlighted = it highlights the button with some flash(in button background) when the user taps.
UIControlStateSelected = it highlights nothing to that button.
From the official doc:
UIControlStateHighlighted 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.
UIControlStateSelected Selected state of a control. For many controls,
this state has no effect on behavior or appearance. But other
subclasses (for example, the UISegmentedControl class) may have
different appearance depending on their selected state. You can
retrieve and set this value through the selected property.
Your button get highlighted in reaction of a touch event. It could then be on a selected state within a group (for segmented control).
Highlighted is typically applied transiently when the control is being touched, selected is a more permanent state. Imagine a checkbox type button which dimmed while it was being touched - dimming is highlighted, ticked is selected, unticked is unselected.
Typically you'd never set highlighted status manually as the system will be setting/unsettling it in response to touches, whereas selected is safer. This particularly applies to buttons.
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 am using SSToolkit and SSCollectionView. When I click a SSCollectionViewItem, I would like to darken the item so the user knows it has been pressed.
The documentation (http://sstoolk.it/documentation/Classes/SSCollectionViewItem.html) shows a setSelected and setHighlighted method, but I am not sure how then to change the appearance of my item.
Any help?
The answer is in the documentation:
Discussion
Highlights or unhighlights the item,
animating the transition between
regular and highlighted state if
animated is YES. Highlighting affects
the appearance of the items's labels,
image, and background.
Note that for highlighting to work properly, you must fetch the item's
label (or labels) using the textLabel
(and detailTextLabel) properties and
set the label's highlightedTextColor
property; for images, get the items's
image using the imageView property and
set the UIImageView object's
highlightedImage property.
A custom table item may override this
method to make any transitory
appearance changes.
SSCollectionViewItem
I am making multiple custom buttons that look much like this:
It is a simple button with either the green or gray in the "indicator view". What I need some explanation for is: In interfacebuilder there are four states a button can have; Normal, Highlighted, Selected and Disabled. When I provide images for everything except disabled I thought that normal would be when no touches were made on the button, highlighted is while you hold your finger on it and selected would be when after you release finger.
However I do not think thats right now. I use the touch-up-inside event. Is it correct that I need to set the selected/highlighted etc property on the button?
Thank you for your time.
You might want to set to selected and not highlighted.
Highlight is darkening the button for a fraction when touching the UIButton. UIButton can modify your image automaticaly so usually you don't need to provide a highlight image.
Disabled is when it is disabled.
Selected is when it is selected. You can invert the select flag on touch up inside event to make a state button.
[button setSelected:![button isSelected]];
Yes, you need to respond to the touch up inside by setting the button to highlighted.
Btw, it's "disabled" not deselected, but it doesn't sound like you need that state.
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