隐藏UIButton标题

kri*_*ris 12 iphone title uibutton ios

我在滚动视图中有几个UIButton,我用它来传递某些信息.信息保存在每个uibutton的标题中,单击该按钮时,它会将其标题传递给该函数.

我想要做的就是隐藏按钮的标题,这样你就看不到按钮了.我把它们叠加在我用来显示按钮的图像上.我将文本设置为透明,但在单击时它仍然变为白色.

如果您在解释中包含代码,请说明它应该去哪里.

Ohm*_*hmy 28

在IOS7之后,如果你想隐藏一个按钮的titleLabel上的标题,你可以按照以下步骤操作.这样标题仍然存在,只是让它看不见.如果您执行NSLog("%@",button.currentTitle),您将在终端中看到标题.希望这可以帮助.

[button setTitle:@"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;
Run Code Online (Sandbox Code Playgroud)


Igo*_*gor 12

我发现只有一种正确的工作方式:

//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)

//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
Run Code Online (Sandbox Code Playgroud)


apo*_*che 11

使用button.titleLabel.hidden = YES不起作用(至少在iOS 7上).

我最终使用:

// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
Run Code Online (Sandbox Code Playgroud)


Sam*_*ami 5

您可以隐藏按钮内的标签:

button.titleLabel.hidden=YES;
Run Code Online (Sandbox Code Playgroud)

或者将按钮的标题设置为@"",并在想要检索时将值保存在其他位置.


Rya*_*uen 5

我创建了 UIButton 的子类并重写layoutSubviews方法。layoutSubviews在方法中隐藏 titleLabel 有效。

public class LoadingButton: UIButton {
    public var isTitleHidden: Bool = false {
        didSet {
           titleLabel?.isHidden = isTitleHidden
        }
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.isHidden = isTitleHidden
    }
}
Run Code Online (Sandbox Code Playgroud)

如果想隐藏titleLabel,只需设置isTitleHidden = true