UIButton label not visible - ios

I'm making a custom UIButton with 2 images, but for some reason the title is just not showing up, the button is showing fine, but without any title.
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(10, 250, 300, 43);
[_button addTarget:self action:#selector(loginClicked:) forControlEvents:UIControlEventTouchUpInside];
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
[_button setTitle:#"Login" forState:UIControlStateNormal];
[_button setTitle:#"Login" forState:UIControlStateHighlighted];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.view addSubview:_button];

These two lines:
[_button setImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
should be
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:normalButtonImg] forState:UIControlStateNormal];
[_button setBackgroundImage:[UIImage imageWithContentsOfFile:hoverButtonImg] forState:UIControlStateSelected];
Explanation: if a foreground image is set for UIButton, the image is placed above the title, so it might hide it. If you want an image and a title to be displayed, you have to set a background image.

When you set the image, that overrides the title. If you can make your image a background image, that will work. Otherwise you will need to create a composite image for the button by essentially creating a new image which is your desired image with the title drawn over it.

Related

buttons turn blue when backgroundimage is set

I have a few buttons and when I set a picture as background the icons turn blue, for example:
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
All pictures are black and white, so I don't understand why they change their color when I set them as a backgroundimage.
Make sure to init the button as a "UIButtonTypeCustom"
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Then if you want the whole button to be an image, you use:
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
... Or if you want to have the image as the background, and still see the "title", you use:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
First do
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
and then set image
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];

Set permanent button image after tap

i've programmatically created a button with an image for normalState.
But I want to set a new button image when the button is pressed (the new image should be displayed for the rest of the time). I tried something, but it only works during tapping. So the button shows its old image after tapping again.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
Thanks for your help.
Try to use this it will helps you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
// ----------- Add this line in your code -------
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
- (void)btnPressed:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
}
You have to do the setting in the target of the button. Add the following code:
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
and set the image in that method for UIControlStateNormal:
- (void)buttonTapped:(UIButton *)sender {
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateNormal];
}

How to make an UIButton with emboss background image?

This is the effect of button on iPad Safari:
Now I have only the icon file like that:
How can I programmatically make an UIButton with the icon image as background look like the Safari on iPad?
Thank you very much!
UIImage *buttonImage = [UIImage imageNamed:#"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
try this one..
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 150, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"bg.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"ar.png"] forState:UIControlStateNormal];
bg.png
ar.png

UIButton set image and title

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.

title not displayed on UIButton

I am using the following code for a custom back button with title in the nav bar. However, the title is not being shown, any help ?
nb: the back button image is black and the same one thats used by default by ios, needed to customize the action handler, thats why did it this way.
backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateSelected];
[backButton addTarget:self action:#selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:#"Encameo" forState:UIControlStateNormal];
[backButton setTitle:#"Encameo" forState:UIControlStateSelected];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];
You are setting image on button instead of this you need to set backgroundImage of UIButton then your title will be displayed.
Change the code like this
backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateSelected];
[backButton addTarget:self action:#selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:#"Encameo" forState:UIControlStateNormal];
[backButton setTitle:#"Encameo" forState:UIControlStateSelected];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];

Resources