'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. 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). 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
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
I can't figure out why this label is there and how to remove it. Should I add the button programmatically instead of adding it through the storyboard? Should the button be treated differently because it's an AR app?
I was able to remove the label by adding the same UIButton as an IBOutlet and adding the following line in viewWillAppear:
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. I tried to add the same workaround line to the function when the button is clicked, but that didn't help.
I'm sure I'm missing something very simple. Your help would be appreciated.
Thanks!
Solution 1:[1]
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.
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. So I found this line using Sublime Text and deleted it there:
<state key="normal" title="Button"/>
After that, it worked correctly.
Solution 2:[2]
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:-
The initial state of the button is assigned as Plane like below:-
Then change the button style property as Default and remove title text for the same like below:-
Solution 3:[3]
I just fill the title with a blank character (using the space bar) and it is good...
Solution 4:[4]
Change the UIButton style to "Default" and clear its title.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | wildcat12 |
Solution 2 | Ashutosh Mishra |
Solution 3 | ?lostA |
Solution 4 | Abdul Karim Khan |