我有3个名为edit_category_left_up的按钮图片,edit_category_middle_up和edit_category_right_up
我已编写代码来创建具有动态宽度的按钮现在我需要使用以上3个图像设置这些按钮的背景,如下图所示
这是我目前创建动态按钮的代码
-(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];
}
答案 0 :(得分:1)
如果您使用以下技术,则只需要单个图像中的背景。
UIImage *backgroundImage = [[UIImage imageNamed:@"MyImageTitle"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, rightInset)];
其中leftInset
是图像左侧不可拉伸内容的宽度,rightInset
是图像右侧不可拉伸内容的宽度。
这将创建一个只覆盖定义的insets内容的图像。这是解决此问题的最佳方法。
或者,如果您无法合并图像,则可以这样做。
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];
您可能需要使用插入子视图的图层。为此,您可以使用以下方法之一:
[btn insertSubview:view aboveSubview:otherView];
[btn insertSubview:view atIndex:someIndex];
[btn insertSubview:view belowSubview:otherView];
答案 1 :(得分:0)
尝试使用此代码组合三个图像
-(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;
}
答案 2 :(得分:0)
您是否只为iOS7写作?
如果是这样,请制作一个“可伸缩”图像,并使用资产目录将其导入项目。
然后,您可以在此处指定目录中可伸缩图像的属性(即上限等...)。
然后正确设置背景图像,您只需使用...
[button setBackgroundImage:[UIImage imageNamed:@"BackgroundImage"] forState:UIControlStateNormal];
这将从资产目录中获取可调整大小/可伸缩的属性。
答案 3 :(得分:0)
只需查看此主题一次。可能这不是你想要的,但我希望它可能对你或其他人有所帮助。