[英]Image and Title to UIbutton is not being set
I'm trying to implement an image as well as title on UIButton. 我正在尝试在UIButton上实现图像以及标题。 Size of UIButton is 86 x 96. I need to embed an image of size 86 x 76 on top and title of size 86 x 20 just below the image.
UIButton的尺寸为86 x96。我需要在顶部嵌入尺寸为86 x 76的图像,并在图像下方嵌入尺寸为86 x 20的标题。
The code is 该代码是
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(76,0,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:@"flower_icon.png"] forState:UIControlStateNormal];
[generalButton setTitle:@"Flower" forState:UIControlStateNormal];
[self addSubview:generalButton];
But the problem is that only image is being shown, whereas the title is not showing up. 但是问题在于只显示了图像,而标题没有显示。 And if I hide the image by commenting the code, then title shows up perfectly.
如果我通过注释代码隐藏图像,那么标题将完美显示。
Please Help 请帮忙
try this code: 试试这个代码:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(30,-50,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setImage:[UIImage imageNamed:@"RepresentativeIcon.png"] forState:UIControlStateNormal];
[generalButton setTitle:@"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];
Please try this code: 请尝试以下代码:
UIButton *generalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[generalButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
generalButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:13];
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
generalButton.frame = CGRectMake(32,20,86,96);
[generalButton setBackgroundColor:[UIColor yellowColor]];
[generalButton setImage:[UIImage imageNamed:@"Facebook"] forState:UIControlStateNormal];
[generalButton setTitle:@"Flower" forState:UIControlStateNormal];
[self.view addSubview:generalButton];
Note: 注意:
[generalButton setImageEdgeInsets:UIEdgeInsetsMake(20,22,20,0)];
[generalButton setTitleEdgeInsets:UIEdgeInsetsMake(50,-30,0,0)];
These two line are performing main role to repositioning your title and image so set all parameters as per your image height & width and title length. 这两行对重新定位标题和图像起着主要作用,因此请根据图像的高度,宽度和标题长度设置所有参数。
In this code I change image name as I have and also changed back colour of button so please change according to your data. 在此代码中,我将更改图像名称,也更改了按钮的背景色,因此请根据您的数据进行更改。
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.