UIButton setImage is not working - objective-c

I am trying to set the UIImage for a UIButton via the following code, but nothing happens. I am setting these inside viewDidLoad. The only way it shows up is if I use storyboard and set the image that way.
- (void)viewDidLoad
{
...
[self.locateMeButton setFrame:CGRectMake(11, 11, 46, 46)];
[self.locateMeButton setImage:[UIImage imageNamed:#"locateme_disabled.png"] forState:UIControlStateDisabled];
[self.locateMeButton setImage:[UIImage imageNamed:#"locateme_active.png"] forState:UIControlStateHighlighted];
[self.locateMeButton setBackgroundImage:[UIImage imageNamed:#"locateme_normal.png"] forState:UIControlStateNormal];
...
}

UIButton *aboutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aboutBtn addTarget:self action:#selector(showAboutPage) forControlEvents:UIControlEventTouchUpInside];
[aboutBtn setTitle:#"About Me" forState:UIControlStateNormal];
[aboutBtn setFrame:CGRectMake(80.0, 120.0, 162.0, 42.0)];
[aboutBtn setBackgroundImage:[UIImage imageNamed:#"locateme_normal.png"] forState:UIControlStateNormal];
[aboutBtn setBackgroundImage:[UIImage imageNamed:#"locateme_disabled.png"] forState:UIControlStateHighlighted];

I had a similar problem. It turned out in my case that I was recreating the toolbar so the wrong toolbar object was being used. (Was trying to access a button on the wrong toolbar). Basically it seems a good idea to just check systematically that your methods are acting on all the receivers you expect and are being passed parameter values that you would expect.

Related

UIButton works with one background image, not with another

I have a UIButton being created programmatically with a background image. Here is the code:
self.shiftb = [UIButton buttonWithType:UIButtonTypeSystem];
[self.shiftb setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[self.shiftb setTitle:#"sh" forState:UIControlStateNormal];
[self.shiftb.titleLabel setTextColor:[UIColor whiteColor]];
self.shiftb.titleLabel.font = [UIFont systemFontOfSize:30];
[self.shiftb setFrame:CGRectMake(0*ss.size.width/10, 2*ss.size.height/4, 1.5*ss.size.width/10,ss.size.height/4)];
[self.shiftb addTarget:self action:#selector(shift) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.shiftb];
As you can see, i'm setting the background image to #"back.png". The above code works perfectly.
if I set it to #"shift1.png" like this:
[self.shiftb setBackgroundImage:[UIImage imageNamed:#"shift1.png"] forState:UIControlStateNormal];
When I use this code, the button appears with no background at all. Both images exist, in the same directory, and they are both the same size. Why could this be happening?

Why are UIButton actions not being called when UIButton is inside a UIScrollView

I have created a springboard like panel containing buttons as follows:
-(void)configurePanel{
self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
(BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
)];
UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
#"Option1",
#"Option2",
#"Option3",
#"Option4",
#"Option5",
#"Option6",
nil
];
for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i;
button.frame = CGRectMake(
(((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
, BUTTON_Y_OFFSET
, BUTTON_WIDTH
, BUTTON_HEIGHT
);
[button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
[button setBackgroundColor:[UIColor grayColor]];
[button.layer setCornerRadius:10.0f];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:#selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
[scrollViewContent addSubview:button];
}
//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
(([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
(BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}
The panel displays correctly and scrolls, but the tap events (sortByButtonTap:) for the buttons are never called. I suspect this is related to the buttons being contained in a view which is contained in the scrollview. After reading a number of other questions and the docs I still can't figure out what the solution should be.
EDIT:
I experimented w/ adding the buttons to the UIScrollView (self.criteriaPanel) directly and the button taps call sortByButtonTap: so it is something to do w/ the buttons being in the scrollViewContent.
You might need to set userInteractionEnabled on the containing view scrollViewContent for the button to receive touch events:
[scrollViewContent setUserInteractionEnabled:YES];
What if you try:
[button setExclusiveTouch:YES];
Buttons tend to work abnormally when they are inside a view that also handles touch events.
Follow this code for your query,
Add UIButton in Scrollview
scrollview.pagingEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake();
[button setTitle:#"X" forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrollview addSubview:button];
scrollview.userInteractionEnabled = YES;
you can put this three methods of button manually.
Let me know if you still have query.
instead of:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchDown];
try:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchUpInside];
I think your problem is just that you set the wrong UIControlEvent to the UIButton. Change from UIControlEventTouchDown to UIControlEventTouchUpInside. If you connect a UIButton inside the Interface Builder it will always set it to this UIControlEvent:
Another reason:
Otherwise your UIScrollView controls the interactions because it needs to know if you want to scroll it. If you place UIButtons inside your UIScrollView they will be accessible but always with a little delay. This is why you should maybe change the settings for the UIScrollView:
You can disable delays content touches and UIButtons will be accessible immediately

UIImage Buttons (Rounded Rect Button)

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.
So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.
How can I do this? Here is the code I have so far.
- (IBAction)btnOne:(id)sender {
result.text = [self appendresult:#"1"];
//Changes the button image upon touch
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
}
Put your code...
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

Special tag for UIButton?

I have 3 different UIButtons for each image grouped. I have IDs for each image. Right now, I have a special id for each image, and I set the button with that tag.
I want to change the background image of the one selected when you tap it. The problem is, is that 3 buttons have the same tag so I cannot change the right button's background image.
Here's what I have:
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setBackgroundColor:[UIColor clearColor]];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateSelected];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateHighlighted];
[likeButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateDisabled];
[likeButton setFrame:CGRectMake(13, 52 + (285 * count), 51, 55)];
[likeButton addTarget:self action:#selector(likeDudle:) forControlEvents:UIControlEventTouchDown];
[likeButton setTag:theIdInt];
[likeButton setTitle:#"no_like" forState:UIControlStateNormal];
[scrollView addSubview:likeButton];
- (IBAction)likeDudle: (id)sender {
NSInteger tagId = ((UIControl*)sender).tag;
UIButton *tempButton = (UIButton*)[scrollView viewWithTag:tagId];
NSLog(#"likeDudle: %d // %#", tagId, tempButton.titleLabel.text);
if ([tempButton.titleLabel.text isEqualToString:#"no_like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button_hit.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"like" forState:UIControlStateNormal];
} else if ([tempButton.titleLabel.text isEqualToString:#"like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:#"icon_like_button.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"no_like" forState:UIControlStateNormal];
}
Is there a better way in doing this?
Thanks,
Coulton
If you have fewer than 10 images, then give the ith button the tag i*10+image.tag. Then you can retrieve the image.tag by button.tag % 10 and the button tags will be unique. You can even retrieve the button only information by int b = button.tag/10.
Also, you can access the button's image's tag with button.backgroundImage.tag, so then the button could have its own separate tagging system, depending on your uses.

Programmatically Create Button to URL

I've managed to create a simple rounded rect button in Xcode through code but I'm having a little trouble actually getting it to do more what I want.
Ideal situation is a custom button type with image in both states as image.png linking to a URL (say www.google.com) can anyone help me out? I presume I need to write a method to do the link. That I can handle, just not sure how to hook it up without Interface Builder (targetting iOS)
Existing Code
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(supporturl) forControlEvents:UIControlEventTouchDown];
btn.frame = CGRectMake(20, 20, 150, 50);
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[self addSubview:btn];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 150.0f, 50.0f)];
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(openWebPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
and in function
-(void) openWebPage
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}