简体   繁体   English

带有自定义图像的 UIButton 仍然显示 titleLabel,即使我将其设置为空白 - Swift iOS

[英]UIButton with custom image still shows titleLabel even though I set it to blank - Swift iOS

I'm pretty new to Swift, currently writing an AR game.我是 Swift 的新手,目前正在编写 AR 游戏。 Seems like my issue is very basic, but I can't figure it out.好像我的问题很基本,但我无法弄清楚。

I added a button to an AR Scene through the storyboard and linked it to an IBAction function (which works correctly when the button is clicked).我通过 storyboard 向 AR 场景添加了一个按钮,并将其链接到 IBAction function(单击按钮时它可以正常工作)。 I gave the button an image and deleted the Title.我给了按钮一个图像并删除了标题。 See how the button shows up in the storyboard: button in Xcode storyboard without Title查看按钮在 storyboard 中的显示方式: button in Xcode storyboard without Title

But when I run the app, the button image shows up with a default label (saying "Button") as shown in this image: button in iPhone screenshot WITH label next to the button image但是当我运行应用程序时,按钮图像显示默认 label(说“按钮”),如下图所示:按钮图像旁边的 iPhone 屏幕截图中的按钮 label

I can't figure out why this label is there and how to remove it.我不知道为什么这个 label 在那里以及如何删除它。 Should I add the button programmatically instead of adding it through the storyboard?我应该以编程方式添加按钮而不是通过 storyboard 添加它吗? Should the button be treated differently because it's an AR app?按钮是否应该因为它是 AR 应用程序而受到不同对待?

I was able to remove the label by adding the same UIButton as an IBOutlet and adding the following line in viewWillAppear:通过添加与 IBOutlet 相同的 UIButton 并在 viewWillAppear 中添加以下行,我能够删除 label:

restartButton.titleLabel?.text = ""

But this feels to me like a workaround and not a real solution.但这对我来说像是一种变通方法,而不是真正的解决方案。 Also, as soon as I click on the button, the label shows up again.另外,只要我点击按钮,label 就会再次出现。 I tried to add the same workaround line to the function when the button is clicked, but that didn't help.单击按钮时,我尝试将相同的解决方法行添加到 function,但这没有帮助。

I'm sure I'm missing something very simple.我确定我遗漏了一些非常简单的东西。 Your help would be appreciated.您的帮助将不胜感激。

Thanks!谢谢!

In iOS-15, Xcode-13.1 we are facing the same issue, You can change the same with Interface Builder via changing the "style" state from "Plane" to "Default" and then removing the title for that button, It will work perfectly fine as shown below:-在 iOS-15、Xcode-13.1 中,我们面临同样的问题,您可以通过将“样式”state 从“平面”更改为“默认”,然后删除该按钮的标题,使用 Interface Builder 来更改相同的问题,它将起作用完全没问题,如下所示:-

The initial state of the button is assigned as Plane like below:-按钮的初始 state 被指定为平面,如下所示:-

在此处输入图像描述

Then change the button style property as Default and remove title text for the same like below:-然后将按钮样式属性更改为默认值并删除标题文本,如下所示:-

在此处输入图像描述

Change the UIButton style to "Default" and clear its title.将 UIButton 样式更改为“默认”并清除其标题。

When Interface Builder isn't playing nice, I often open the Storyboard file in a text editor (I use Sublime Text) and edit it manually.当 Interface Builder 运行不佳时,我经常在文本编辑器(我使用 Sublime Text)中打开 Storyboard 文件并手动编辑它。

I had a similar issue - I had a button with an image, I had deleted the default "Button" title text in IB, which looked fine in Xcode, but when I ran it, the word "Button" was still there.我有一个类似的问题 - 我有一个带有图像的按钮,我删除了 IB 中默认的“按钮”标题文本,这在 Xcode 中看起来不错,但是当我运行它时,“按钮”这个词仍然存在。 So I found this line using Sublime Text and deleted it there:所以我使用 Sublime Text 找到了这一行并在那里删除了它:

<state key="normal" title="Button"/>

After that, it worked correctly.之后,它工作正常。

I just fill the title with a blank character (using the space bar) and it is good...我只是用一个空白字符(使用空格键)填充标题,这很好......

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

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