Place a Button over an Image Programmatically - ios

I coded this:
UIButton *buttonPrint = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonPrint setImage:[UIImage imageNamed:#"stampa.png"] forState:UIControlStateNormal];
buttonPrint.frame = CGRectMake(15, 5, 117, 41);
[buttonPrint addTarget:self action:#selector(print) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonPrint];
//button mail
UIButton *buttonMail = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonMail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
buttonMail.frame = CGRectMake(0, 46, 117, 41);
[buttonMail addTarget:self action:#selector(sendEmail) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonMail];
//button facebook
UIButton *buttonPublish = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonPublish setImage:[UIImage imageNamed:#"pubblica.png"] forState:UIControlStateNormal];
buttonPublish.frame = CGRectMake(20, 90, 117, 41);
[buttonPublish addTarget:self action:#selector(publish) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonPublish];
//button twitter
UIButton *buttonTweet = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonTweet setImage:[UIImage imageNamed:#"tweet.png"] forState:UIControlStateNormal];
buttonTweet.frame = CGRectMake(4, 130, 117, 41);
[buttonTweet addTarget:self action:#selector(tweet) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonTweet];
- (void)sendEmail {
NSLog(#"mail clicked");
}
- (void)tweet {
NSLog(#"tweetClicked");
}
- (void)print {
NSLog(#"printClicked");
}
- (void)publish {
NSLog(#"face clicked");
}
but the buttons does not work... What should i do??

UIImageView has a property userInteractionEnabled. It's disabled by the default, so if you are adding youre buttons as a subviews of the image view they will not respond. If this is your issue. you can enable that flag or consider to move the buttons to the image view's superview.
Hope it helps. Cheers.

Related

Why is UIButton blurry when creating programatically

i created a uibutton like so:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(280, 320, 100, 30);
[button setTitle:#"World" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];
and the button's text looks edgy, someone know why?
Change
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
^^^^^^
All will be good..
Try giving Font :
// Configure the cell...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 30);
[button setTitle:#"World" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.font=[UIFont fontWithName:#"HelveticaNeu-CondensedBold" size:16];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];

UIButton Image is not changed in Hightlight or Selected state

I wrote the following code of a button for an iOS8 app:
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted|UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnBack];
And the method when click:
-(void) btnBackClick:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
However, the background of the image does not change when the button is being click. The only effect is the grayout.
If I draw that button on a xib file, the image change works like a charm.
Anything wrong? Please help.
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted|UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[btnBack setSelected:YES];
[self.view addSubview:btnBack];
//**********Updated*******
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnBack];
-(void)btnBackClick:(UIButton *)btn
{
if ([btn isSelected])
{
[btn setSelected:NO];
}
else
[btn setSelected:YES];
}
-(void) btnBackClick:(id)sender{
UIButton *myButton = (UIButton *)sender;
[myButton setSelected: YES]; // Add this line
[self.navigationController popToRootViewControllerAnimated:YES];
}
Also in viewWillAppear method set the same as NO
[myButton setSelected: NO]; // Add this line

Change label when a button has been pressed

I have a button which changes images which it has been pressed, it goes from img1 to img2. I now also want the text to be changed when that certain button been pressed. So it starts on "hello", and then when pressed it turns to "goodbye" for example.
this is my code at the moment:
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
ViewDidLoad:
[self addLabel];
self.btn1ImageName = #"greypad.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(46, 67, 48, 48)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
The label?
-(void)addLabel{
UILabel *aLabel = [[UILabel alloc]initWithFrame: CGRectMake(20, 200, 280, 80)];
aLabel.numberOfLines = 0;
aLabel.textColor = [UIColor blueColor];
aLabel.backgroundColor = [UIColor clearColor];
aLabel.text = #"hello";
[self.view addSubview:aLabel];
}
Try this
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
self.titleStr = ([self.titleStr isEqualToString:#"hello"]) ? #"goodbye" : #"hello";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 setTitle:self.titleStr forState:UIControlStateNormal];
}
I would recommend a different approach:
When you set up your button, set different title and image for different states
[btn1 setTitle:#"hello" forState: UIControlStateNormal];
[btn1 setTitle:#"goodbye" forState: UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:#"greypad.png"] forState:UIControlStateNormal];
[btn1 setImage:[UIImage imageNamed:#"greenpad.png"] forState:UIControlStateSelected];
And in your toggle method simply do
- (void)toggleImage:(UIButton *)button
{
button.selected = !button.selected;
}
Add this to your toggleImage Method:
[bt1 setTitle:#"goodbye" forState: UIControlStateNormal];

UIButton won't change picture when HighLighted, will only set picture to grey

I want the picture to turn yellow the second the user touches it. Right now on first tap the image turns a bit grey and on release turns yellow.
Second tap the picture would go back to red instantly. How can i fix this?
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
I ran your code and it's working fine mate. Just double check the image names!
EDIT 1:
replace your code with the followings:
-(void)bMethod:(UIButton*)sender
{
if (sender.imageView.image != [UIImage imageNamed:#"red.png"]){
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateNormal];
}
else{
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateNormal];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
EDIT 2:
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
sender.highlighted = !sender.highlighted;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchUpInside];
myButton.adjustsImageWhenHighlighted = NO;
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}

xcode invisible button with text

I know I could do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
but that leaves a border around the button.
I could also do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then put a UILabel behind the button.
Is there a way to accomplish this ^ without needing two objects?
UPDATE:
To clarify my question:
I want the have text, only text, that when pressed performs a method call. For reasons that are complicated to explain I need to do this by way of a button. So, how do I make the text of a button the only visible part of the button. No border. No background. Just text.
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:#selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
no need to take label object.
I can't get what you want exactly but you might be missed [self.view addSubview:button] in your code. so you can not display button with text.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Your second code is working fine. In the below image hello is a button.
My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
in this way u can do this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:#"Button text" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];

Resources