简体   繁体   English

如何在UIButton上设置不可见的标题?

[英]How to set an invisible title on UIButton?

I'm working on an Ipad application, and i need to set a title on a 'UIButton', in order to could use : 我正在开发Ipad应用程序,并且需要在“ UIButton”上设置标题才能使用:

-(void)MyMethod:(id)sender
{
    UIButton *resultButton = (UIButton *)sender;
    if ([resultButton.currentTitle isEqualToString:@"TitleName"])
    {
        ...
    }
}

But when I use this sort of code : 但是当我使用这种代码时:

[MyUibutton setTitle:@"TitleName" forState:UIControlStateNormal];

It appears "TitleName" behind the image of the 'UIButton'... 出现在“ UIButton”图像后面的“ TitleName” ...

So, is it possible to fix it ? 那么,有可能修复它吗?

Thanks ! 谢谢 !

Don't use the button title, it's for display, not a flag. 不要使用按钮标题,它是用于显示,而不是标志。 Instead use a tag or objc_setAssociatedObject Or store the button in a dictionary and compare. 而是使用tagobjc_setAssociatedObject或将按钮存储在字典中并进行比较。

theButton.titleLabel.alpha = 0;

If you set the image of the button, it will be at the top of the button, so the button's title won't be visible. 如果设置按钮的图像,它将位于按钮的顶部,因此按钮的标题将不可见。 Set the button's background and it will solve your problem ! 设置按钮的背景,它将解决您的问题!

[yourbutton setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];

You can do [button setTitle:title forState:UIControlStateDisabled]; 您可以执行[button setTitle:title forState:UIControlStateDisabled]; as long as you know you are not ever going to disable the button. 只要您知道您永远不会禁用该按钮。 Other than that, using the tag property is probably the easiest. 除此之外,使用tag属性可能是最简单的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM