How to set background for UIButtons with dynamic width? - ios

I have 3 button images named edit_category_left_up,edit_category_middle_up and edit_category_right_up
I have written code to create buttons with dynamic width now I need to set background of these button like this image below using the above 3 images
This is my current code to create dynamic buttons
-(void) addDynamicButtonsForCategories
{
_adcImage.hidden=YES;
_makeADCLabel.hidden=YES;
float x=0; float y=0; float frameHeight=_subADCView.frame.size.height;
NSString *titleString=#"";
for(int i = 0; i < 15; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
if (i<1)
{
[btn setFrame:CGRectMake(0, 0, 39, 39)];
[btn setImage:[UIImage imageNamed:#"addcategory_up.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"addcategory_down.png"] forState:UIControlStateHighlighted];
x = x + btn.frame.size.width+10; y=y+btn.frame.origin.y;
[btn setTitle:titleString forState: UIControlStateNormal];
[btn setTag:i];
}
else
{
titleString = [titleString stringByAppendingString:[NSString stringWithFormat:#"%d",i]]; // get button title
CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];
CGRect currentFrame = btn.frame;
CGRect buttonFrame = CGRectMake(x, y, fontSize.width + 22.0, 40);
if ((buttonFrame.origin.x+buttonFrame.size.width) >_subADCView.frame.size.width)
{
x=0;y=_subADCView.frame.size.height;
_subADCView.frame=CGRectMake(_subADCView.frame.origin.x, _subADCView.frame.origin.y, _subADCView.frame.size.width, _subADCView.frame.size.height+frameHeight);
buttonFrame = CGRectMake(x, y, fontSize.width+ 22.0, 40);
}
x = x + fontSize.width + 35.0; y=y+currentFrame.origin.y;
[btn setFrame:buttonFrame];
//I need to set the button image here
[btn setImage:[[UIImage imageNamed:#"edit_category_middle_up.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateNormal];
[btn setImage:[[UIImage imageNamed:#"edit_category_middle_down.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateHighlighted];
[btn setTitle:titleString forState: UIControlStateNormal];
[btn setTag:i];
lastButtonPosition=btn.frame.origin.y+btn.frame.size.height;
}
[self.subADCView addSubview:btn];
}
[self readjustSubviews];
}

You only need the background in a single image if you use the following technique.
UIImage *backgroundImage = [[UIImage imageNamed:#"MyImageTitle"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, rightInset)];
where leftInset is the width of unstretchable content from the left side of the image and rightInset is the width of unstretchable content from the right side of the image.
This will create an image that only stretches content inside the defined insets. This is the best way to solve this problem.
Alternatively, if you cannot possibly combine your images then you can do it like this.
UIImage *leftImage = [UIImage imageNamed:#"edit_category_left_up"];
UIImage *middleImage = [UIImage imageNamed:#"edit_category_middle_up"];
UIImage *rightImage = [UIImage imageNamed:#"edit_category_right_up"];
UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, leftImage.size.width, btn.frame.size.height)];
[leftImageView setImage:leftImage];
UIImageView *middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftImageView.frame.size.width, 0, btn.frame.size.width - (leftImage.size.width + rightImage.size.width), btn.frame.size.height)];
[middleImageView setImage:middleImage];
UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(middleImageView.frame.origin.x + middleImageView.frame.size.width, 0, rightImage.size.width, btn.frame.size.height)];
[rightImageView setImage:rightImage];
[btn addSubview:leftImageView];
[btn addSubview:middleImageView];
[btn addSubview:rightImageView];
You might need to play around with the layer the subviews are inserted at. To do that you can use one of these methods:
[btn insertSubview:view aboveSubview:otherView];
[btn insertSubview:view atIndex:someIndex];
[btn insertSubview:view belowSubview:otherView];

Try this code to combine three images
-(UIImage*)combineImages
{
UIImage *image1 = [UIImage imageNamed:#"test.png"];
UIImage *image2 = [UIImage imageNamed:#"yyu.png"];
UIImage *image3=[UIImage imageNamed:#"test.png"];
//self.imageView.image=image3;
CGSize size = CGSizeMake(image1.size.width+image2.size.width+image3.size.width, image1.size.height );
UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0,0,image1.size.width, size.height)];
[image2 drawInRect:CGRectMake(image1.size.width,0,image2.size.width, size.height)];
[image3 drawInRect:CGRectMake(image1.size.width+image2.size.width, 0,image3.size.width,size.height)];
UIImage *finalImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}

Are you writing for iOS7 only?
If so make a single "stretchable" image and import it into the project using an Asset Catalog.
From here you can then specify the properties of the stretchable image (i.e. cap insets etc...) in the catalog.
Then to set the background image correctly you just use...
[button setBackgroundImage:[UIImage imageNamed:#"BackgroundImage"] forState:UIControlStateNormal];
This will take the resizable/stretchable properties from the asset catalog.

Just look into this thread once. May be this is not for what you are looking, but I hope it may be helpful for you or someone else.
Build a UIImage with multiple stretchable images

Related

How to set the background image for vertical UISlider in Objective-C?

I am developing an application in iOS , and I have created a vertical UISlider by following code:
UISlider *BrightnessSlider;
BrightnessSlider = [[UISlider alloc] initWithFrame:CGRectMake(24, 132, 118, 34)];
BrightnessSlider.minimumValue = 0;
BrightnessSlider.maximumValue = 100;
BrightnessSlider.value = (BrightnessSlider.minimumValue + BrightnessSlider.maximumValue) / 2;
BrightnessSlider.continuous = YES;
BrightnessSlider.transform =CGAffineTransformMakeRotation(M_PI*1.5);
[BrightnessSlider addTarget:self action:#selector(BrightnessSliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:BrightnessSlider];
And tried to setting the background image of UISlider by using the following code:
UIImage *clearImage = [[UIImage imageNamed:#"SliderBar.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[BrightnessSlider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
[BrightnessSlider setMaximumTrackImage:clearImage forState:UIControlStateNormal];
But the image is coming as horizontal , did I missing something ?
Thanks in advance.
-------------------------------EDIT--------------------------------------
The code is like the following:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"brightness_slider.png"] drawInRect:self.view.bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRotateCTM (context, radians(90));
UIImage *sliderimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[BrightnessSlider setMinimumTrackImage:sliderimage forState:UIControlStateNormal];
[BrightnessSlider setMaximumTrackImage:sliderimage forState:UIControlStateNormal];
The image of UISlider is like the following picture;

UIButton text and image

I have one UIButton. In that UIButton i have to show text and image. Image should be displayed after the text. Based on text i want to adjust the button frame.
See the image. After the text "I am a Button", i want to add a image like arrow.
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *name = #"I am a button";
titleButton.titleLabel.text = [NSString stringWithFormat:#"%#", name];
titleButton.titleLabel.textColor = [UIColor whiteColor];
CGSize stringSize = [name sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"MyriadPro-Cond" size:24]}];
CGRect frame = CGRectMake(300, 30, stringSize.width, 30);
//frame.size.width = stringSize.width;
[titleButton setFrame:frame];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"home.png"]];
image.frame = CGRectMake(stringSize.width, 14, 20, 13);
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, stringSize.width+20, 0);
[titleButton addSubview:image];
I written code like this. But it is not working. Anyone can make it correct. Thanks in advance
You dont have to add a UIImageView over the UIButton to get this behaviour. The UIButton has itself has some properties. Set the orange image as the background image of the button, set the arrow image as the image of the button. Then adjust the insets of the title and the image of the button to move the title to the left and the arrow image to the right.
[btn setBackgroundImage:[UIImage imageNamed:#"orange_bg"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"arrow"] forState:UIControlStateNormal];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -60, 0)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 125, 0)];
You have to adjust the insets according to your image and button sizes.

UIButton changes image created in code when in highlighted state

I have two buttons. They both have the same image except the fact that one of the images is flipped. I don't want to have an excess image in the bundle of my app if it's possible to create image programmatically so i create my buttons like this:
UIImage *forwardImage = [UIImage imageNamed:rewind_btn_img];
UIImage *rewindImage = [UIImage imageWithCGImage:forwardImage.CGImage
scale:forwardImage.scale
orientation:UIImageOrientationUpMirrored];
NSArray *images = #[rewindImage, forwardImage];
for (int i = 0; i < 2; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = images[i];
btn.frame = CGRectMake(0.f, 0.f, image.size.width, image.size.height);
btn.center = CGPointMake(self.playButton.center.x + (i == 0 ? - 80.f : 80.f) * TGScaleMultiplier, self.playButton.center.y);
btn.tag = i + 1;
[btn setBackgroundImage:image forState:UIControlStateNormal];
[btn addTarget:self action:#selector(rewindButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
The problem is that when i press the button whose image is rewindImage it shows the original image, flipped to the other side. Am i doing something incorrectly here? Is there any workaround?
Not sure exactly why that UIImage:imageWithCGImage is flaky but I tried another method for mirroring the image and when applied to the button it works great. So get rid of the UIImage:imageWithCGImage line and use this:
UIImage *rewindImageBase = [UIImage imageNamed:rewind_btn_img];
UIGraphicsBeginImageContext(rewindImageBase.size);
CGContextRef current_context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(current_context, rewindImageBase.size.width, 0);
CGContextScaleCTM(current_context, -1.0, 1.0);
[rewindImageBase drawInRect:CGRectMake(0, 0, rewindImageBase.size.width, rewindImageBase.size.height)];
UIImage *rewindImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
That should do it.
if it initially looks right, but changes when you press it, Try setting the image for all button states, or at least for the normal state and the selected state.
[btn setBackgroundImage:image forState:UIControlStateNormal];
[btn setBackgroundImage:image forState:UIControlStateSelected];
you could also try setting the showsTouchWhenHighlited but the first way should work
[btn setShowsTouchWhenHighlighted:NO];
EDIT: try also setting the highlighted state, this works for me
[btn setBackgroundImage:image forState:UIControlStateHighlighted];

how to change uiimageview image's programmatically

In my ios application i create uiimageviews programmatically like that,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
in my buttonActon, i want to change that uiimageview's image to another image. But i can not do that. Can anyone help me? Thank You.
try like this ,
for (int i=0; i<20; i++) {
productID=[products objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
//[button setImage:redButtonImage forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = 100+productID;
[scrollView addSubview:button];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=productID;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
}
keep this one in button action method and pass the imageview tag in the place of imgview.tag
UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
img.image=[UIImage imageNamed:#"name.png"];
Instead of this:
imageView.tag=UrunId;
Write:
imageView.tag = productID + 100;
Because you need a unique tag for each Image View.
Then implement the buttonAction: like:
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
[imageView setImage:yourImage];
}
Please try to use this one
You should have different tag for button and image.So please give first different tag like this....
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
imageView.tag=button.tag+1;
UIImage *image = [UIImage imageNamed:#"redImage.png"];
[imageView setImage:image];
[scrollView addSubview:imageView];
After that do this...
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:sender.tag+1];
imageView.image=[UIImage imageNamed:#"imageName"];
}

UIBarButtonItem with UIImage too wide

I have a UINavigationBar and a UIToolbar (in separate views) which contain buttons and flexible space. The UIBarButtonItems' background is set like this:
bgImage = [[UIImage imageNamed:#"MyBackgroundImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[self setBackgroundImage:bgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Within the UINavigationBar, the items look just fine, optimal size and all.
BUT, within the UIToolbar, the items are always stretched to at least the width of bgImage (100 points in this case):
Any ideas why, or how to solve that? Please tell me if you need more info.
This appears to be a bug in UIToolbar. I gave it a try and the only "fix" that worked for me was to manually set the width of the UIBarButtonItem:
barButtonItem.width = 40f;
This will work fine for buttons with images, but not for text buttons as the size of the text may vary due to localization.
You can use following method for get/create image size as you want.
Use following method with specific hight and width with image
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
CGSize newSize = CGSizeMake(width, height);
float widthRatio = newSize.width/image.size.width;
float heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return NewImage, with specific size that you specify.
And use this Image for add on UIBarButtonItem.
Create your own button with image according to your requirement
// UIBarButtonItem+CutomBackground.h
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString *)buttonTitle target:(id)target action:(SEL)action;
// UIBarButtonItem+CutomBackground.m
#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString *)buttonTitle target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [image stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[button setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:FONT_SIZE]];
[button.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = [buttonTitle sizeWithFont:[button.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN;
buttonFrame.size.height = IMAGE_HEIGHT;
[button setFrame:buttonFrame];
return [[self alloc] initWithCustomView:button];
}

Resources