iOS: How to cover entire button with image - ios

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;

Related

changing text of custom navigation bar item

I am creating a custom UINavigationBar BarButtonItem as follows:
// Set up bar button items
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[backButton setImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(myselector) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
This is working perfectly, but when I go to set the title of the UIButton or BarButtonItem, it won't show up. Any suggestions?
Tried...
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
And...
self.navigationItem.leftBarButtonItem.title = #"Edit";
without luck.
The problem is that image and title use same viewspace for display.
So when we use image with title first it show image and than title (right side of image).
If size of UIButton is same as image than we can't see Title.
A simple solution is set image in background. So try this:
[backButton setBackgroundImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
UIImage *image = [UIImage imageNamed:#"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

Create custom UIBarButtomItem in storyboard

I need to make custom "Settings" button in navigation bar.
But, unfortunately, I was unable to make in in storyboard editor correctly.
I need to see only my picture, no button rectangle.
Is there are a way to make with kind of button correctly?
Thanks.
You can add this code in the ViewController -viewDidLoad method:
for the left bar button:
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[leftButton setImage:buttonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[leftButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
or for the right bar button:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[rightButton setImage:buttonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[rightButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
There's no need to add a BarButtonItem to your view in the xib or storyboard.
Here's what it could look like:

Add text to a custom Back Button iOS

I have a backbutton with an image I'm using in my navigation controller, and I'd like to set the text over top of the image. I tried this but it doesn't seem to show up:
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.bounds = CGRectMake(0, 0, normalBackImage.size.width, normalBackImage.size.height );
[backButton setImage:normalBackImage forState:UIControlStateNormal];
backButton.titleLabel.text = #"Back";
Does anyone know how to accomplish this? Thanks!
set the title's color, else you'll not see anything :)
setTitleColor:forState:
2.
dont try to set the image (which means FOREGROUND image). Instead set the BACKGROUND image :)
– setBackgroundImage:forState:
sample:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 10, 100, 50);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitle:#"Bla" forState:UIControlStateNormal];
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
[backButton setBackgroundImage:normalBackImage forState:UIControlStateNormal];
[self.view addSubview:backButton];
You could create a UILabel and add it as a subview to your UIImage. That would look something like this (writing code outside the compiler, might not be perfect)
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:#"My Label Text"];
[image addSubview:myLabel];

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar
Not written again in every interface.
Is there a unified method to uniform setting?
UIImage image = [UIImage imageNamed:imagePath];
UIButton button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
A lot of interface backBarButtonItem need custom,code is the same,I don't want to write N times
You can add this in a singleton global-variables file, like
GlobalVariable.h
+(UIBarButtonItem*)customButton;
GlobalVariable.m
+(UIBarButtonItem*)customButton{
UIImage *image = [UIImage imageNamed:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithCustomView:button];
return btn;
}
Then, import GlobalVariable.h in the views you want the button to appear and call
self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];
You can keep GlobalVariable there, in case you need other variables too.

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