How to change button image added to TableView section header - ios

I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}

I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.

I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}

You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}

Related

How to change Button image added to UITableview section header in ios

Here is my code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
button.frame = CGRectMake(sectionView.frame.size.width-10, 10, 20, 20);
[button addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:button];
}
- (void)btnAction:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
}
First thing set images of button for both state (UIControlStateNormal, UIControlStateSelected) in ViewDidLoad and then do [cell.btnSelect setSelected:YES]; or [cell.btnSelect setSelected:NO]; as per requirement.
Otherwise change image for UIControlStateNormal when button tapped.
you are creating UIButton each time header load, which cause your button to reset,
1 way is to design cell in storyborad or xib and use reference,
Also have a DataSource to keep track of values
e.g.
if([dataSouce containsObject:yourItem]){
//selected button image
[cell.btnSelect setSelected:YES];
}
else{
//unselected button image
[cell.btnSelect setSelected:NO];
}

Perform three different action for a single button

I want to perform three different actions for a single button. I surfed in google also it shows some different options but those are not helpful.
I have declared NSInteger inside CustomUIButton i.e. click and initialize inside IBAction to 0.
Here is the code.
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
NSInteger flag = 0;
button.click=0;
if(button.click==0)
{
button.click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (button.click==1)
{
button.click=2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (button.click==3)
{
button.click=0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
But it is not working. Can someone tell me where I am doing wrong.
Define button.click=0; globally so initially its value will be zero and after that it will increase.
#interface yourclass : superclass
{
int click;
}
- (void)viewDidLoad{
click=0;
}
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
NSInteger flag = 0;
if(click==0)
{
click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (button.click==1)
{
click=2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (button.click==3)
{
click=0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
You are setting the button.click = 0 for every action.
So every time you click it is being set to zero.
You can instead keep a separate variable to count in your view controller than having a click property on button. Hope this helps :-)
According to your code,
it was always goes on
if(button.click==0)
{
button.click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
I will give you better suggestion instead of "click" you can use "tag" option which was the one property of uibutton.
First you have to define a custom button on .h file and then set the tag = 0 in - (void)viewDidLoad
method.
CustomUIButton *button;
NSInteger flag = 0;
button.click=0;
use it this way or as simple from all such problems use some other variable as
because every time this function is call its zero every time so make it outside this function and use it
NSInteger flag = 0;
int counter = 0;
-(void)changeStudentAction:(id)sender
{
if(counter ==0)
{
counter =1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (counter ==1)
{
counter =2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (counter ==3)
{
counter =0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
in start button tag to zero then change as following
-(void)changeStudentAction:(id)sender{
CustomUIButton *button=sender;
NSInteger flag = 0;
if(button.tag==0)
{
button.tag=1;
}
else if (button.tag==1)
{
button.tag=2;
}
else if (button.click==3)
{
button.tag=0;
}
}
Please try following code may be solve your problem.
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
[UIView setAnimationDuration:0.4];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
[UIView setAnimationDuration:0.6];
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[UIView commitAnimations];
}

I want to change the UIButton Highlight Image, but without success

I have a UIbutton in my view
I want to change the UIButton Highlight Image, but without success
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 66, 29)];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- (void)filterAction:(UIButton *)button
{
if(button.isSelected == YES)
{
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
button.selected = NO;
}
else
{
#warning I want to change the UIButton Highlight Image, but without success
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_normal.png"] forState:UIControlStateHighlighted];
button.selected = YES;
}
}
If you want your button to behave as follows,
normal - filter_button_normal
highlighted (on touch press down on button) - filter_button__down_selected
selected (after removing touch from button) - filter_button_selected;
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateHighlighted];
so when the user touches it , image changes to highlighted and when he removes his finger over it, it changes to selected
And the target you added is UIControlEventTouchUpInside
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
so filterAction is called after the user removes his finger from it.
- (void)filterAction:(UIButton *)button
{
// the image will change automatically
if(button.isSelected == YES)
{
button.selected = NO;
}
else
{
button.selected = YES;
}
}

I have to perform togling for single UIButton which I have generated from loop for different frame

In my iOS application, I have to change image as per button click.
I have to perform togling for single UIButton which I have generated from loop for different frame
It is working fine if I perform action on single button, if I have performing action for multiple button the image is not properly changing as per button action
Here is my button
btnFullScreen = [[UIButton alloc] init];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[btnFullScreen addTarget:self action:#selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:btnFullScreen];
Here is my button action code
-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];
}
Thanks
I had done this before in one of my app.
I was creating button in each tableview cell.
So,Take this code as Reference.
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Resources