gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:50 25 4
gpt4 key购买 nike

我有 3 个按钮图像,分别名为 edit_category_left_up、edit_category_middle_up 和 edit_category_right_up enter image description here enter image description here enter image description here

我已经编写了代码来创建具有动态宽度的按钮,现在我需要使用上面的 3 张图片设置这些按钮的背景,如下图所示

enter image description here

这是我当前创建动态按钮的代码

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

最佳答案

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

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

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

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

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

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

您可能需要尝试插入 subview 的图层。为此,您可以使用以下方法之一:

[btn insertSubview:view aboveSubview:otherView];

[btn insertSubview:view atIndex:someIndex];

[btn insertSubview:view belowSubview:otherView];

关于ios - 如何为具有动态宽度的 UIButtons 设置背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com