UIButton set image and title - ios

I use the following code to load an image is on the upper, lower half is the title of the button, but only the image, is this why? Have any good Suggestions please let us know, thank you
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 44)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets: UIEdgeInsetsMake(0,0,22,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,10,0,10)];
[self.view addSubview:myButton];

you can achieve this effect by this way also.. Try this ::
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 55)];
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIImageView *imgVw = [[UIImageView alloc ] init];
imgVw.image = img;
imgVw.frame = CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myButton.frame.size.width, (myButton.frame.size.height-18));
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(34,10,0,10)];
[myButton addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imgVw];
[self.view addSubview:myButton];
Your button method
-(void) btnClick:(id)sender
{
NSLog(#"working");
}
You can achieve from your logic also :: Here i've edited some code in your logic
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 40, 35)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0,(myButton.frame.size.width-52),17,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,(myButton.frame.size.width-78),0,22)];
[self.view addSubview:myButton];
but, in this logic you have to resize your image to 23*23 pixels. according to your image size UIEdgeInsetsMake's parameter will be changed.
Hope this helps. Happy coding.

Related

RightBar Button on navigation bar is not giving click like look

I want to show image and the title on RightBar Button so i have written code as below:
UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
UIImage *buttonImage = [UIImage imageNamed:#"select-ipad#3x.png"];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setImage:buttonImage forState:UIControlStateNormal];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[selectButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[selectButton sizeToFit];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
[selectButton addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];
This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.
-(void)viewWillAppear:(BOOL)animated{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;
}
-(IBAction)onBtnRightClick:(id)sender
{
NSLog(#"Right Bar Button Clicked.");
}
Using your code, try 2 buttons:
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[button2 sizeToFit];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
self.navigationItem.rightBarButtonItems = #[buttonItem2,buttonItem1];
I hope this can help you.

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