I am creating buttons programmatically. I want to change button background color while touching up inside again it has to set back to its usual color after lifting up our finger.....
nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:#"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:#selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nine];
// to change background color
-(void)changeButtonBackGroundColor:(id) sender
{
[nine setBackgroundColor:[UIColor redColor]];
}
Here changeBackgroundColor method was created to change color of that button . it changes color.
Don't know if this relates to your question but:
this
[nine setBackgroundColor:[UIColor redColor]];
should be
[sender setBackgroundColor:[UIColor redColor]];
Edit:
Change this
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
[nine addTarget:self action:#selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchCancel];
and add the method:
- (void)resetButtonBackGroundColor: (UIButton*)sender {
[sender setBackgroundColor:[UIColor cyanColor]];
}
I think you have two options..
First:
You can put the [nine setBackgroundColor:[UIColor redColor]]; inside "clickDigit" (and like dasdom said rename to sender and change to (UIButton*)sender)..
Change
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
and the method "changeButtonBackGroundColor"
[sender setBackgroundColor:[UIColor cyanColor]];
Second
create universal UIControlEvents
[nine addTarget:self action:#selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventAllEvents];
-(void)changeButtonBackGroundColor:(UIButton*) sender{
if ([sender.backgroundColor isEqual:[UIColor redColor]]){
[sender setBackgroundColor:[UIColor cyanColor]];
}else{
[sender setBackgroundColor:[UIColor redColor]];
}}
I didn't try this code
Related
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?
I created a custom button as shown in below:
-(void)addYellowBtn{
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow3.jpg"]forState:UIControlStateNormal];
//sets background image for highlighted state
[customButton setBackgroundImage:[UIImage imageNamed: #"yellow5.jpg"] forState:UIControlStateHighlighted];
[customButton setFrame:CGRectMake(60, 100, 60, 60)];
[customButton setTitle:#"" forState:UIControlStateNormal];
CALayer *btnLayer = [customButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:12.0f];
[self.view addSubview:customButton];
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
}
when i click this button, next view will have to push.To do this,how can i write codings?
Change this:
[customButton addTarget:self action:(btnclick1:) forControlEvents:UIControlStateNormal];
to this
[customButton addTarget:self action:(btnclick1:) forControlEvents: UIControlEventTouchUpInside];
UIControlStateNormal is not a touch event.
I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.
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"]];
}
I'm attempting to create a view with two UIButtons. The code compiles without any error but the buttons don't have any labels and they can't be clicked.
-(id)initWithTabBar {
if ([self init]) {
self.title = #"Tab1";
self.tabBarItem.image = [UIImage imageNamed:#"promoters.png"];
self.navigationItem.title = #"Nav 1";
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(facebookAuthorize) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(10, 10, 100, 100)];
[self.view addSubview:button];
[button release];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:#"Button" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
[button2 setFrame:CGRectMake(110, 10, 100, 100)];
[self.view addSubview:button2];
[button2 release];
return self;
}
Don't release those buttons. They are created as autoreleased objects. As described in documentation:
You only release or autorelease
objects you own. You take ownership of
an object if you create it using a
method whose name begins with “alloc”,
“new”, “copy”, or “mutableCopy”
None of these words appear in buttonWithType:, so you don't take responsibility and you are safe to assume it's autoreleased.
As mentioned earlier, the buttons cant be named when it is withType. The code to make the words appear in the uibutton is:
UIButton *someButton=[[[UIButton alloc]initWithFrame:CGRectMake(140, 6, 175, 36)]autorelease];
[someButton addTarget:self action:#selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
[someButton setTitle:#"Button" forState:UIControlStateNormal];
[self.view addSubview:someButton];