我想创建一个使用其setBackgroundImage
、setImage
和setTitle
属性的UIButton
,但是当我将setTitle
与setImage
和setBackgroundImage
结合使用时,标题没有出现。
我尝试了以下方法。
[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,请单击标签并转到编辑器>排列 -> 发送到前端。