繁体   English   中英

UIButton显示BackgroundImage,Image和Title

[英]UIButton to show BackgroundImage, Image and Title

我想创建一个使用其setBackgroundImagesetImagesetTitle属性的UIButton ,但是当我将setTitlesetImagesetBackgroundImage结合使用时,标题不会出现。

我尝试了以下内容。

[button setBackgroundImage:[UIImage imageNamed:@"image3.jpg"] forState:UIControlStateNormal]; 
[button setImage:[UIImage imageNamed:@"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:@"%d",[button tag]] forState:UIControlStateNormal]; 
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]]; 
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];

按钮和标题上的标题你的所有图像都在那里,所以在图像上添加标签。

UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:@"label"];

Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];

您的所有图片仅在标题上方,因此您的标题位于背景中,因此请在图片视图上使用标签。

如果使用界面构建器制作GUI,则单击标签并转到编辑器 - >排列 - >发送到前面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM