uibuttontypedetaildisclosure custom image - ios

Is it possible to change the image of UIButtonTypeDetailDisclose from the default one to something which is more interesting. However, notice that I'm asking for button, rather for one type of uibutton.

you will have to change the accessoryView of the cell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath
// code for initializing the cell..
UIImage *image = [UIImage imageNamed:#"interestingImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(44.0, 44.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(accessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}

Related

How Can I Change TableView Header Button Image On Click. Need code in Objective c

This is my code, just added the image but not changing when hit the button.
Please suggest a working code.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
const CGRect fr = CGRectMake(0, 0, 320.0, 40.0 );
btn = [[UIButton alloc]initWithFrame:fr];
UIImageView *imgArrow = [[UIImageView alloc]initWithFrame:CGRectMake(380, 2.5, 25, 25)];
imgArrow.image=[UIImage imageNamed:#"arrow.jpeg"];
[btn setTitle:[self tableView:tableView titleForHeaderInSection:section] forState:UIControlStateNormal ];
btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[btn setBackgroundColor:[UIColor redColor]];
[btn setTag:section];
[btn addSubview:imgArrow];
[btn addTarget:self action:#selector(sectionOpenToggle:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
First, buttons already have an image view that you can use, so don't do this:
[btn addSubview:imgArrow];
Instead, do this:
[btn setImage:[UIImage imageNamed:#"arrow.jpeg"] forState:UIControlStateNormal];
Then, in your sectionOpenToggle method (assuming it looks like this), change the image:
- (void)sectionOpenToggle:(UIButton *)sender {
[sender setImage:[UIImage imageNamed:#"other-arrow.jpeg"] forState:UIControlStateNormal];
// whatever else you want to do
}

uibutton background image not changing objective c

I have table view with custom cell class.
Inside cell I've five uibuttons and in its custom class I've made outlets for all of them and have only one function to handle their tap events like this.
- (IBAction)starClicked:(id)sender {
UIButton *btn = (UIButton*)sender;
switch (btn.tag){
case 501:
self.starOne = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starOne setBackgroundImage:[UIImage imageNamed:#"day-time-temp-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
[self.starOne setTitle:#"23" forState:UIControlStateNormal];
[self.starOne setNeedsLayout];
self.starTwo = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starTwo setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starThree = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starThree setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFour = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFour setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFive = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFive setBackgroundImage:[UIImage imageNamed:#"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
break;
...
}
}
by using tag property I am checking which button is tapped, and it is working fine as I have checked it using break point. and when I press first button I want to change image of first button. And on second button I want to change image of first and second button. It is like a rating control. I want to change star image with highlighted image of star.
but below code not working. I have tried setting setNeedLayout too. But image is not changing.
Edit
this is my cell for row function.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuestionsCell *cell = [tableView dequeueReusableCellWithIdentifier:#"questCell"];
if(cell == nil){
cell = [[QuestionsCell alloc] init];
}
[cell customizeCellWithQuestions:[self.questionList objectAtIndex:indexPath.row] textSize:1 index:indexPath.row];
return cell;
}
This is my customizeCellWithQuestions function inside my QuestionCellClass.
-(void)customizeCellWithQuestions:(Question *)question textSize:(float)textSize index:(NSUInteger)index{
[self.questionTitleTv setText:[question.question capitalizedString]];
[self.questionIndexLbl setText:[NSString stringWithFormat:#"%lu",(unsigned long)index+1]];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor colorWithRed: 213.0/255.0 green: 213.0/255.0 blue: 213.0/255.0 alpha: 0.0];
}
You need to set Image for one state individually.
Using OR operator won't work
[self.starOne setBackgroundImage:[UIImage imageNamed:#"day-time-temp-icon.png"] forState:UIControlStateNormal];

Adding an Image to my UIButton subview

I have a UIButton that is added on my super view as a subview, It is just a big block, how do I add an image to my button.
- (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}
you can add image in UIButton by following two ways.
1) With help of setting Background Image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
2) Or you can add UIImageView as subview.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];
[button setClipsToBounds:YES];
[button setBackgroundImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
UIImageView * imgView = [[UIImageView alloc]init];
[imgView setImage:[UIImage imageNamed:#""]];
[imgView setFrame:CGRectMake(0, 0, 100, 100)];
[button addSubview:imgView];
[self.collectionView.superview addSubview:button];
[button addTarget:self action:#selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
Use this:
-setBackgroundImage:forState:
you may looking for something like this, if you want to have an "image as button" and not only a default button with an image inside
BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
CGRect frame;
//universal app
if (ipad)
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
else
{
btn.frame = frame = CGRectMake( x, y, w, h );
}
btn.titleLabel.backgroundColor = [UIColor clearColor];
forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
There are different ways to do it.
First one is you can add an imageview to your superview and set imageview's image whatever you want. Then, at the same position add your button with size of imageview and set button color to clearColor. But you need to add imageView first otherwise your button will be under the image.
Second one is default image to button, use setImage:image forState:UIControlStateNormal or setBackgroundImage:image forState:UIControlStateNormal.
Got it, here is how:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
Use this method.
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

how to add the UITableView detailDisclosurebutton custom in ipad app

I have tableView with detailDisclosure button it works fine but i want to give title to detail disclosure button so i think i need to add custom UIButton for detailDisclosure is it possible to do like i want to do this without using custom cell.
If i want to give image it works fine but how may i give title to this button like comment.
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"loginN.png" ]];
Try this..
UIButton *btn = [[[UIButton alloc]init]autoerelease];
btn .frame = CGRectMake(210.0, 0.0, 90, 25);
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"loginN.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubView: btn];
and in btnClickHandler do this to get indexPath
- (void)btnClickHandler :(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Get your data source object from indexPath.row.
}
Why don't u set the a button as accessory view just like u try setting an image..??
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);//Set the frame u need for the view
//Customize the button
[button setTitle:#"title" forState:UIControlStateNormal];
//And set it as accessory view
cell.accessoryView = button;
Here is the result...

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

Resources