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)
您可以隐藏按钮内的标签:
button.titleLabel.hidden=YES;
Run Code Online (Sandbox Code Playgroud)
或者将按钮的标题设置为@"",并在想要检索时将值保存在其他位置.
我创建了 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