Height and Width of the UIbutton on a cell changes on Click - ios

I have 2 questions here.
1.)
In my cellForRowAtIndexPath i have added the following code.
[cell.comB addTarget:self action:#selector(comBClick:) forControlEvents:UIControlEventTouchUpInside];
When the user clicks on the button in that cell the following method gets fired. However, the position of the UIbutton after clicking changes. The button slips down a bit. How can i prevent this ? When i debugged i noticed that the button position changes after executing [((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:#"m.png"] forState:UIControlStateSelected];
line.
- (void) comBClick:(id) sender {
if(c%2==0)
{
[((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:#"m.png"] forState:UIControlStateNormal];
} else {
[((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:#"n.png"] forState:UIControlStateNormal];
}
c=c+1;
}
2.
How can i find the width and height of a UIButton ?
UPDATE
I have got the following AutoLayout Warning, which i believe is the reason for the issue. Can someone tell me what's wrong and how to solve it ?

2)
This is the easiest way:
float widht = ((UIButton *)sender).bounds.size.width;
float height = ((UIButton *)sender).bounds.size.height

Related

How to change button's Image on clicking it in ios

Confession: I have been through every possible link I could find, but unfortunately none work for me.
I am creating buttons dynamically. When a button is selected, it should change the color of the selected button and the rest should remain the default color. This way, the user can identify which button is selected.
Suppose there are 3 buttons: all blue in color, and when I select the first one, it should change to white and other two should remain blue. When I select the second, the first one should go back to blue and the second should now be white.
btnCounts data I am fetching from server and would vary.
-(void)createButtons{
for (int i=0; i<btnCounts; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:i];
[btn setFrame:CGRectMake(xVal, 0, width/btnCounts, 40)];
// [btn setImage:[UIImage imageNamed:#"btn_image.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"white.png"] forState:UIControlStateSelected];
[btn setTitle:name forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrV addSubview:btn];
xVal += self.view.frame.size.width/btnCounts+1;
}
}
Now, in btnClicked: method I am passing the button as a parameter, so that I can use selected button.
-(void)btnClicked:(UIButton *)button
{
int tag = (int)button.tag;
//...
}
Please help me to find what I am missing here.
Thanks.
Create an instance UIButton variable to house the selected button so that you can deselect it when the next button is tapped. If you only pass the currently-tapped button to your btnClicked: method, you won't know which button to deselect (if any).
#implementation ClassName {
UIButton *previousButton;
}
...
- (void)btnClicked:(UIButton *)button
{
if(previousButton)
[previousButton setBackgroundColor:[UIColor blueColor]];
previousButton = button;
[button setBackgroundColor:[UIColor whiteColor]];
// do whatever else you need to do here
}
For that you need to store selected button in temporary object.
Create button in interface(.h) file:
UIButton *selectedButton;
Than add following line in -(void)createButtons method before for loop.
selectedButton = nil;
for() {...}
Here selectedButton is set to nil because initially none of the buttons are selected.
And finally replace your -(void)btnClicked:(UIButton *)button method with following method.
-(void)btnClicked:(UIButton *)button {
if(selectedButton) {
selectedButton.selected = NO;
}
button.selected = YES;
selectedButton = button;
}
If you want to make any button initially selected than assign that button to selectedButton like in btnClicked method.

UIButton Selected State Image Freezes on Autorotation

I have a UIButton in the footer of UITableView. It has an empty checkbox as its unselected state, and a checked box as its selected state. It works perfectly in portrait, but if the view ever rotates to landscape, the button's image is frozen in whatever state it was when it rotated. The button still sends messages correctly after rotation, but the image does not change.
self.theButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.theButton setImage:checkbox
forState:UIControlStateNormal];
[self.theButton setImage:checkedBox
forState:UIControlStateSelected];
This code appears in the tableView:viewForFooterInSection: of my view controller. In the action linked to the button, I set self.theButton.selected = !self.theButton.selected;
What's going on?
You can do this in your button action
- (void)didTapCheckbox:(id)sender {
UIButton *btn =(UIButton *)sender;
if (btn.selected == YES) {
[btn setSelected:NO];
}
else{
[btn setSelected:YES];
}
}
instead of your code just do this
if(self.theButton == nil){
self.theButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.theButton setImage:checkbox
forState:UIControlStateNormal];
[self.theButton setImage:checkedBox
forState:UIControlStateSelected];
}

UIButton does not change background on touchUpInside

I have some custom type UIButtons placed pragmatically in a custom view. The view is basically a menu. I am generating the buttons with the following code, inside the view's .m file:
-(void) generateButtons: (NSMutableArray *) buttonsArray
{
UIImage *barItemImage = [[UIImage imageNamed:#"ipad-menubar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
float buttonWidth = (self.frame.size.width - (2 * FIRSTBUTTONXCOORDINATE) - 25)/ 6.0f;
float xCoord = FIRSTBUTTONXCOORDINATE;
float yCoord = (self.frame.size.height - BUTTONHEIGHT) / 2.0f;
for (int i = 0; i < buttonsArray.count; i++)
{
NSString* buttonTitle = [buttonsArray objectAtIndex:i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(botButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:buttonTitle forState:UIControlStateNormal];
button.frame = CGRectMake(xCoord, yCoord, buttonWidth, BUTTONHEIGHT);
button.tag = i;
[button setBackgroundImage:barItemImage forState:UIControlStateNormal];
xCoord += buttonWidth + 5;
[self addSubview:button];
}
}
On touchUpInside, botButtonAction is called and it does what it is supposed to. However, the buttons do not usually give any visual indication of the touchUpInside. When I touchDown on a button and swipe in any direction, then I see the background color get darker. Sometimes, I see the same visual indication on touchUpInside. It seems completely random though.
Any ideas as to what I might be doing wrong here?
Edit: Sorry, I got occupied with other issues. Here is the code for botButtonAction and the method it calls
-(void) botButtonAction: (UIButton *) sender
{
if (self.delegate)
[self.delegate optionsMenuAction:self data:sender.tag];
}
-(void)optionsMenuAction:(OptionsMenueView *)view data:(int)tag
{
if (self.PDFVC)
{
[self.navigationController popToViewController:self animated:YES];
}
if (!self.optionsMenue.isAtWPC || tag != WPC)
[self transitionFromViewAtIndex:self.selectedVCIndex toView:tag];
else
[self transitionFromViewAtIndex:self.selectedVCIndex toView:WPList];
}
But since I am using touchUpInside, these methods don't get called on touchDown. I just want the button to be highLight (grayout the bg a little) on touchDown. As I state before, that happens on touchDown and swipe.
Edit
I had so far been running this on actual devices (iPad air and iPad 2). When I tried running it on the simulator, I get the correct result. On mouse click, the button gets highlighted.
Edit
I think I figured out the exact problem (still looking for a solution). If I touchDown precisely on the button and the touch does not cover any part of the insets, then the highlighting works. However, if the touch covers part of the button and also part of the insets/outside, then the highlighting doesn't work.
add this line:
[button setBackgroundImage:HighlightImage forState:UIControlStateHighlighted];
change the title colour when tap on the button like this
[yourButon setTitleColor:[UIColor colorYouWant] forState:UIControlStateHighlighted];
hope this will help you......
put this code in botButtonAction:
-(void)botButtonAction:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"some iamge.png"]
forState:UIControlStateNormal];
}
or use the code below code in for loop
[button setBackgroundImage:[UIImage imageNamed:#"back_button_press.png"] forState: UIControlStateHighlighted];
or if you want to give some color then setcolour of button
I had this problem at some point too. These other answers will give you the result you seek normally. However.... You are using [UIButton buttonWithType:UIButtonTypeCustom].. Which is really buggy if you do not alloc/init the UIButton.. Try changing this line to a normal button allocation and then add the following lines of the answers in this post. This should work for you.
So in short,
replace [UIButton buttonWithType:UIButtonTypeCustom];
with [[UIButton alloc] init];
And then add the following changes to your code to change the image.
[myNewlyAllocatedButton setBackgroundImage:[UIImage imageNamed:#"back_button_press.png"] forState: UIControlStateHighlighted];

Unrecognised selector sent to instance When changing UIButton background

Hey I am writing a function that handles favorites within my app, as well as changing the image of the favorite button I want to change the image of a corresponding button in another UIScrollView DoubleScrollLeft.
The below code works HOWEVER I get the following eror when trying to favorite my first button with the tag '0' is there a reason for this? (the rest work).
-[UIScrollView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x14e3b0
Also once the images do change it pushes the title of the button off to the right, do i need to reset the frame etc.. when i change the background for state?
-(void)favButtons:(id)sender {
int i = [sender tag];
NSString *fav = [NSString stringWithFormat:#"%i", i];
if ([[Favinsults objectForKey:fav] isEqualToString:#"0"]){
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD1.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:#"favButton0.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD0.png"] forState:UIControlStateNormal];
}
}
There seems to be a mis-connection where a UIScrollview is connected to the IBAction method. Check your IB connections carefully.
Your sender is somehow UIScrollview.
Change this line as, This should avoid the warning message.
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
[((UIButton *)sender) setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
You have to cast the sender to a UIButton before you can call any of the UIButton methods on it...
UIButton * newbutton = (UIButton*)sender;
[newbutton setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
etc.
[UIScrollView setBackgroundImage:forState:]
means that your sender is interpreted to be of type UIScrollView (not UIButton), and there is no method setBackgroundImage:forState: defined in UIScrollView.

UIButton - On touch change image

When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the UIButton object. [Use the images as per your requirement]
[myButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
#7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore"]
forState:UIControlStateNormal];
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:#"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.

Resources