试图在图像按钮上显示文本

时间:2013-04-15 23:40:38

标签: ios

我正在尝试在按钮上显示文字,但没有任何显示。

 LocationButton = [[UIButton alloc] init];

 UIImage * Location_buttonImages = [UIImage imageNamed:@"button~iphone.png"];

 [LocationButton setTitle:@"Loca" forState:UIControlStateNormal];
 [LocationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

 [LocationButton setImage:Location_buttonImages forState:UIControlStateNormal];
 [LocationButton setImage:[UIImage imageNamed:
 @"button_select~iphone.png"]  
 forState:UIControlStateSelected];

  LocationButton.frame =  CGRectMake(20,212,90,40);

 [LocationButton addTarget:self action:
 @selector(action:) forControlEvents:UIControlEventTouchUpInside];

  [self.view addSubview:LocationButton];

我可以看到图像,但没有文字可见。请帮助!

1 个答案:

答案 0 :(得分:0)

 [LocationButton setImage:Location_buttonImages forState:UIControlStateNormal];
 [LocationButton setImage:[UIImage imageNamed:@"button_select~iphone.png"]  forState:UIControlStateSelected];

您将图像叠加在标题上,这样就不会显示标题。更改为setBackgroundImage:forState

 [LocationButton setBackgroundImage:Location_buttonImages forState:UIControlStateNormal];
 [LocationButton setBackgroundImage:[UIImage imageNamed:@"button_select~iphone.png" forState:UIControlStateSelected];