如何为具有动态宽度的 UIButtons 设置背景?

Posted

技术标签:

【中文标题】如何为具有动态宽度的 UIButtons 设置背景?【英文标题】:How to set background for UIButtons with dynamic width? 【发布时间】:2013-12-02 07:50:06 【问题描述】:

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

【问题讨论】:

类似这个1..***.com/questions/12002908/… 方法 stretchableImageWithLeftCapWidth:topCapHeight: 已弃用。使用 resizableImageWithCapInsets: 代替,指定 cap insets 使得内部是 1x1 区域。 【参考方案1】:

如果您使用以下技术,您只需要单个图像中的背景。

UIImage *backgroundImage = [[UIImage imageNamed:@"MyImageTitle"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, leftInset, 0, rightInset)];

其中leftInset 是图片左侧不可拉伸内容的宽度,rightInset 是图片右侧不可拉伸内容的宽度。

这将创建一个仅在定义的插图内拉伸内容的图像。这是解决此问题的最佳方法。

或者,如果您无法合并图像,那么您可以这样做。

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

【讨论】:

【参考方案2】:

试试这段代码合并三张图片

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


【讨论】:

【参考方案3】:

你是只为 ios7 写的吗?

如果是这样,制作一个“可拉伸”图像并使用资产目录将其导入项目中。

然后,您可以在此处指定目录中可拉伸图像的属性(即帽插图等...)。

然后要正确设置背景图像,您只需使用...

[button setBackgroundImage:[UIImage imageNamed:@"BackgroundImage"] forState:UIControlStateNormal];

这将从资产目录中获取可调整大小/可拉伸的属性。

【讨论】:

【参考方案4】:

只需查看此线程一次。可能这不适合您正在寻找的东西,但我希望它可能对您或其他人有所帮助。

Build a UIImage with multiple stretchable images

【讨论】:

以上是关于如何为具有动态宽度的 UIButtons 设置背景?的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UITableView 部分标题动态设置背景颜色?

如何为 JTextField 设置动态宽度?

如何为这些 UIButtons 设置边距

目标c如何为整个应用程序上的所有UIbuttons设置独家触摸

如何为 QPushButton 的背景颜色设置动画(动态更改按钮颜色)

如何为具有动态高度的表格单元格正确设置自动布局?