Button background color is repeating in the uicollectionview cell - uibutton

I have a like button on my cell (that has been connected to the CustomCVCell.h file) and I need to change the background of this button when it gets pressed.For that I written this code.
Collectionviewcell.h
(IBAction)secondlikebuttn:(id)sender;
In collectionviewcell.m
- (IBAction)secondlikebuttn:(id)sender {
UIButton *buttonObj = (UIButton*)sender;
[buttonObj setBackgroundImage:[UIImage imageNamed:#"like_icon_inst1_red"]
forState:UIControlStateNormal];
_commview.hidden=YES;
_shareview.hidden=YES;
_likeview.hidden=NO;
}
when click on the button the background image of the button at selected idexpath is changing but the problem is it is reflecting the change in other indexpath also.
Thanks for quick responses.

Related

NSButton with clickable image and text

I've created an NSButton (see code below). The button displays the way I want (borderless, with image above text). If I click the text portion of the button the button will press down and call it's action. If I click the image portion of the button then absolutely nothing happens. How can I make the image portion of the NSButton clickable? I'm sure I can subclass the NSButtonCell to make this work, but I don't understand why that would be necessary.
NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect];
[button setBezelStyle:NSRegularSquareBezelStyle];
[button setBordered:YES];
[button setImagePosition:NSImageAbove];
[button setButtonType:NSMomentaryChangeButton];
[button setImage:[NSImage imageNamed:#"myImage"]];
[button setTitle:#"Button Title"];
[button setTarget:self];
[button setAction:#selector(buttonAction:)];
The image on the button itself. No need to make it clickable. Your click event will be caught by buttonAction.
You only need to show the animation that image got clicked. For that, you need another inverse image. And track the mouse down event, when you click down show the other image, when you release mouse (mouse up) show the default image.
This will give the feel of click on the image.
Maybe some other view overlaps your button's image? In code above, of course if you provide appropriate button frame, all works fine (for both, bordered and borderless button). Try it in clean project.

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

How to add an image in UITableView's content view while we reodering cells?

I have a tableview where I provide the option to reoder the cells. But, I don't want to show the delete icon when the user doing rearranging cells. So I did like this to remove the delete button.
In normal mode..
After edit mode (That is, non deletable but rearrangable mode)..
Here, two things are happening. One is the accessoryview of each cell has a image(that represents rearrangement). Second thing is, all strings are moved some points from the left screen. This gap is actually for the deletion symbol. Since I hide the delete button, an empty space appears.
Ok, here is the problem comes. I now want to add some custom button with an image in each cell's content view.
So, I added the following code in my tableView: cellForRowAtIndexPath:
if (isEditable)
{
selectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"blue_button.png"];
selectionButton.frame = CGRectMake(-25, 10, img.size.width, img.size.height);
[selectionButton setBackgroundImage:img forState:UIControlStateNormal];
[selectionButton addTarget:self action:#selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:selectionButton];
}
The blue button was 24*24 pixels.
The reason why I used -25 as "x" value:
If I give a positive value or zero, the button will overlapped with cell text. Below image represents when the x value is 0.
But for x=-25,
What I need:
Since I added the button with negative x value, the whole image width (24 pixels) will be hiden in the x value (25 pixels). So the button action buttonClickAction: was not called. I want to change the image when the user tapping on it(switching between selected/unselected mode).
Is there any way to call the button's action method? Or should I create a custom cell?
Just confused..
Add this to your Table View Controller
- (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
It will remove empty space.

How to change background image on a button , iOS?

I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that?
I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:#"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button?
Thank you very much for reading my post :D
you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:#"myBackgroundImage.png"] forState:UIControlStateHighlighted];
A button has two properties image and backgroundImage..
For setting image use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];
First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:#"imagename.type"] ForState:UIControlStateNormal];
One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom".
This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.
Hope this helps.
You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).
Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).
Setbutton
if u need image while clicking
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateHighlighted];
or if u want image after Selecting it
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateSelected];
and on that onclick function mention
as
snapshotCheckbox.Selected=YES;
To set an image on a button, just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

iPhone Buttons Question

Once I tap the button, its default highlight color is blue. How can you change the highlight color of a button. Either in interface builder or by code.
In IB when you select the Button Attributes of your UIButton choose "Highlighted State Configuration" (instead of "Default State Configuration") and select an image for the Background.
Or in code: [myButton setBackgroundImage:[UIImage imageNamed:#"imagename"] forState:UIControlStateHighlighted];
So you rather set an image, than set a color.
You might want to look into code examples of creating your custom UIButton ([UIButton buttonWithType:UIButtonTypeCustom]), using UIImage's stretchableImageWithLeftCapWidth:topCapHeight: instance method.