隐藏 UIButton 标题
我在滚动视图中有几个 UIButtons,我用它们来传递某些信息。该信息保存在每个 uibutton 的标题中,单击按钮时,会将其标题传递给函数。
我想做的就是隐藏按钮的标题,这样您就看不到该按钮。我将它们覆盖在用于显示按钮的图像上。我将文本设置为透明,但单击时它仍然变成白色。
如果您在解释中包含代码,请解释它应该放在哪里。
I have several UIButtons in a scrollview which I use in order to pass certain information. The information is saved in the title of each uibutton and when the button is clicked, it passes its title into the function.
All I want to do is hide the title of the button so you can not see the button. I have them overlaid over images which I use to show buttons. I have the text set to transparent but it still turns white when it is being clicked.
If you include code in your explanation, please explain where it should go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在IOS7之后,如果您只想隐藏按钮的titleLabel上的标题,您可以执行以下操作。这样标题仍然存在,只是使其不可见。如果您执行 NSLog("%@",button.currentTitle) 您将在终端中看到标题。希望这有帮助。
After IOS7, If you want to just hide the title on titleLabel of a button you can do as follow. This way the title is still there it just makes it invisible. if you do NSLog("%@",button.currentTitle) you will see the title in terminal. Hope this helps.
我发现只有一种正确的工作方式:
I found only one correct working way:
使用
button.titleLabel.hidden = YES
将不起作用(至少在 iOS 7 上)。我最终使用了:
using
button.titleLabel.hidden = YES
will not work (at least on on iOS 7).I ended up using:
您可以将标签隐藏在按钮内:
或者将按钮的标题设置为 @"" 并在您想要检索该值时将其保存在其他位置。
You can hide the label inside the button:
or set the button's title to @"" and save the value somewhere else when you want to retrieve it.
我创建了 UIButton 的子类并重写
layoutSubviews
方法。在layoutSubviews
方法中隐藏 titleLabel 是有效的。如果想隐藏 titleLabel,只需设置
isTitleHidden = true
I create a subclass of UIButton and override
layoutSubviews
method. Hiding titleLabel inlayoutSubviews
method works.if wanna hide titleLabel, just set
isTitleHidden = true
我无法从
titleLabel
或整个视图中删除标题,因为我需要它作为约束。我最终使用
隐藏标题并
再次显示它
I could not remove the title from the
titleLabel
nor the whole view as I needed it for constraints.I ended up using
to hide the title and
to show it again
要临时隐藏标题,只需将Title设置为空字符串,
按钮标题标签将被隐藏,但标题仍将位于titleLabel中,您可以使用以下命令将其返回
To hide a title temporary just setTitle to empty string
the button title label will be hidden but the title will still in the titleLabel, you can return it back using
如果您想在禁用按钮时临时隐藏标题,请使用:
然后,当您想隐藏标题时,
button.isEnabled = false
。If you want to temporary hide the title, while you disabling the button, use:
Then,
button.isEnabled = false
, when you want to hide the title.我遇到了标题问题,因为使用了属性标题,但上面没有任何帮助。然后我找到了一个解决方法:
但是它有一些缺点,但适合我的需求。
I got a problem with title, because used an attributed title and nothing above helped. Then i found a workaround:
However it has some disadvantages, but fit my needs.
我提出了这个解决方案,它允许您设置标题标签文本并将其与按钮图像一起使用,而不显示它,也不将按钮图像移动到左侧。
I've come up with this solution, which allows you to set title label text and use it with button image without showing it and not moving button image to the left.
您无法使用
.hidden
属性隐藏UIButton
titleLabel
。相反,你可以这样做。隐藏:
显示:
You cannot hide
UIButton
titleLabel
using.hidden
property. Instead you can do this.To Hide:
To Show:
在 Swift 中,
您不需要隐藏也不需要将不透明度设置为 0.0。 Swift 给了你一个更简单的方法。
只需将标题设置为零即可。事实上,我是从文档中得到这个想法的。
命令单击
setTitle(_:for:)
方法,您将看到 -所以,我只是将其设置为 nil。
In Swift-
You don't need to hide nor need to make the opacity to 0.0. Swift gave you a simpler way.
Just set the title as nil. In fact, I got the idea from the documentation.
Command click on the
setTitle(_:for:)
method and you will see-So, I just set it to nil.
Swift 5隐藏按钮标签:
这里myButton是按钮的@IBOutlet。
Swift 5 to hide button label:
Here myButton is a @IBOutlet of the button.