簡體   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