3

如何在 UIButton 中将 UIImage 设置为正常状态并将标题设置为选定状态?

嗨,我尝试在 UIButton 的正常状态下设置 UIImage,但是当在正常状态下设置 UImage 时,这会更改 UIButton 在选定状态下的标题,从而更改图像的标题。我在用

myButton setTitle:content forState:UIControlStateSelected

处于选中状态。

如何在 UIButton 中将 UIImage 设置为正常状态并将标题设置为选定状态?

PDT:对不起我的英语!

4

6 回答 6

3

尝试这个:

选择布尔值;

-(IBAction)btnClicked:(UIButton *)sender{
    if(selected)
    {
        [chkBtn setTitle:@"selected" forState:UIControlStateNormal];
        chkImage.image=[UIImage imageNamed:@" "]; 
        NSLog(@"%@",chkBtn.titleLabel);
        selected=NO;
    }
    else
    {
        [chkBtn setTitle:@" " forState:UIControlStateNormal];
        chkImage.image=[UIImage imageNamed:@"button_minus_pink.png"]; 
        selected=YES;
     }

}

于 2013-02-28T06:03:38.230 回答
1

// 设置为UIColor 

[self.button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

// 设置为UIImage

[self.button setBackgroundImage:[UIImage imageNamed:@"BlackRightBtn.png"] forState:UIControlStateNormal];

为了Button Selected

if (self.button.isSelected == YES)
{
    [self.button setTitle:@"Your_ButtonTitle" forState:UIControlStateNormal];
}
于 2013-02-28T04:54:12.623 回答
1

像这样添加您的代码:

[myButton setTitle:@"Your String" forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateSelected];

[myButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[myButton setTitle:@"" forState:UIControlStateNormal];

img.png 不应该在你的包中

您也可以通过 Interface Builder 进行设置。

于 2013-02-28T07:02:14.257 回答
1

您可以定义按钮和setImage正常和选定状态。像这样。

buttonJobs = [UIButton buttonWithType:UIButtonTypeCustom ];
buttonJobs.tag = 1;
[buttonJobs setTitle:@"Jobs" forState:UIControlStateNormal];


buttonJobs.frame = CGRectMake(2, 56, 109, 33);
[buttonJobs setImage:[UIImage imageNamed:@"tabButtonImage_Off.png"] forState:UIControlStateNormal];//buttonJobs.png

[buttonJobs setImage:[UIImage imageNamed:@"tabButtonImage_On.png"] forState:UIControlStateDisabled];

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

在按钮选择器方法中,您可以设置您选择的按钮sender's tag

-(void)btnNextTab:(UIButton*)sender
{

  if(sender.tag == 1)
  {
      sender.selected = YES;
  }

}

对于标题,您可以更改文本的颜色。

希望这会帮助你。

于 2013-02-28T05:54:05.873 回答
0

检查此代码:

[myButton setTitle:content forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];
[myButton setImage:yourImage forState:UIControlStateNormal];
于 2013-02-28T05:08:41.577 回答
0

试试这个代码:

[myButton setTitle:@"Your String" forState:UIControlStateSelected];
[myButton setImage:nil forState:UIControlStateSelected];

[myButton setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[myButton setTitle:@"" forState:UIControlStateNormal];
于 2013-02-28T05:45:10.250 回答