I have an issue with the background image of button which is dependent of button state. I have a button inside customized table view cell and i have set different images for button's different states. Please look into the code below.
[btnNow setImage:[UIImage imageNamed:#"now_norm.png"] forState:UIControlStateNormal];
[btnNow setImage:[UIImage imageNamed:#"now_focus.png"] forState:UIControlStateHighlighted];
Whenever i tap on the actual button this works great but if i tap on area outside of button but which is still inside that same cell then this button changes its background image from UIControlStateNormal to UIControlStateHighlighted.
If i remove background image from UIControlStateHighlighted state then this issue doesn't exists but i need pressed state of the button.
Please help me out.
Thanks in advance!
Vivek Dandage.
Try with setting the cell.selectionStyle to UITableCellSelectionStyleNone
I wonder how to comment on karim's Answer. But that should be marked as right. It is the answer to the problem. I know, because I was having the same problem and setting the selectionStyle of the cell to None was the solution.
Related
I am showing my my table view in edit mode. So for all rows on left side of table view, there are delete button and by clicking on that it changes its image and showing "Delete" button on right side. I want click event for button available at left side. Please help.
If you are using your custom button as a subview then you can addTarget to your button like this:
[button addTarget:self action:#selector(yourMethod) forControlEvents:UIControlEventTouchUpInside];
Please be specific or show some screenshots. So that I can figure it out.
You cannot achieve this without subclassing UITableViewCell. Please review this answer.
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 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
HI ALL,
I have created two custom buttons using IB and i have set their background image.Now i want that when a user selects a button its background image should change and the new background image should persists until user presses the other button.how to do this?
You have to manage the states of your buttons by yourself in this case.
Meaning that you should hold a BOOL member for each button that will state if the button is selected.
Or, if you should have only one selected button in a time, then you might hold a reference to the selected button.
In the tap events you should manage the states above by changing the image of the last selected button to non-selected image and the image of the currently selected button to selected button image.
You can change the image like this:
[button setBackgroundImage:[UIImage imageNamed:#"selected_button.png"] forState:UIControlStateNormal];
You can combine 2 methods to do so:
- (void)setImage:(UIImage *)image forState:(UIControlState)state
call it using [self.button setImage:YOUR_IMAGE forState:UIControlStateSelected];
Then you can set the button to be selected. [self.button setState:UIControlStateSelected] . When another button is selected, you set the state back to normal.
I have a UIButton subview inside of a UITableViewCell.
When this button is touched, the user must hold the button for about a half second for the button's image to change to the UIControlStateHighlighted image.
This means that if the user just taps the button as is usually the case, the highlighted state is never shown.
Why does this occur and how can I fix it?
I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.
Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.
The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.
The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.
If you don't need a table view, get rid of the UITableView.
Up for David Hodge's answer.
I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.
SOLUTION
Subclass UIScrollView (or UITableView as the original question) and override:
-(BOOL) touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.
I just change the image from within the target action method:
[sender setBackgroundImage:[UIImage imageNamed:#"highlighted-image.png"] forState:UIControlStateNormal];
It changes the background image instantly.
Edit: completely re-written following a misunderstanding of the question
One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.
UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.
UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// SEt up title, frame etc
[myButton addTarget:self action:#selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown];
[myMainView addSubView:myButton];
Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.
If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.