How to make an UIButton with emboss background image? - ios

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

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];

iOS: How to cover entire button with image

I am currently working on a iOS application where I am constantly changing the pictures of two buttons. The problem is that the picture of the button that constantly changes does not stretch over the entire button. I was wondering what is the best way of doing this.
I believe I am setting my image of my button normally and I cant figure out what to do. Any suggestions will be greatly appreciated.
leftButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [orangeImageArray objectAtIndex:randomPicture2]]] forState:UIControlStateNormal];
Is your button type UIButtonTypeCustom ?
example :
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:btnImage forState:UIControlStateNormal];
Try this:
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[leftButton setImage:btnImage forState:UIControlStateNormal];
This code enables you to customize the button well:
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn addTarget:self action:#selector(prefer) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barButton;

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];
}

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.

ImageView and UIButton

I would like to create a button with a custom image ( background image), i have done this.
how can assign the imageview to may button ?
thanks for your answers
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
What you want is:
[button setBackgroundImage:image forState:UIControlStateNormal];
or
[button setImage:image forState:UIControlStateNormal];
#izan you dont need a separate UIImageView to show on the UIButton.
A UIButton comes with 2 UIImageView inside it. you can just set the image for these 2 imageViews.
You can set the image for the first imageview using setImage:forState: method. By doing this you cannot show any title for that button. If you want to show title as well as image on the button you should use setBackgroundImage:forState: method.
Use the setImage method;
- (void)setImage:(UIImage *)image forState:(UIControlState)state
So your code would be;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[button setImage:image forState:UIControlStateNormal];
Or I you still want your text to be shown use;
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
Which would result in;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(goToGeoloc) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
UIButton documentation;
http://bit.ly/kruq5y
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goToGeoloc)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"ico_plan.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(245.0, 8.0, 45.0, 45.0);
Hope this helps

Resources