22

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!

rafi
  • 389
  • 2
  • 10
  • First, good question. I personally don't like using IB or Storyboards, but basically a `UIButton` is the same class - be it in code or in IB. So to answer a part of your question, it shouldn't matter is it's an AR app. There is *nothing* you can do in IB that cannot be done through code, and as you've already said, it may be a workaround. One thought... try putting a space in the title property in IB. (Maybe even removing it after that.) –  Nov 15 '21 at 21:11
  • It has been a minute since I have used IB but I would try changing the button type to 'Custom' from the drop-down. – Eric Murphey Nov 15 '21 at 21:15
  • ```restartButton.setTitle(""., for: .normal)``` try setting your title like this in the ```viewDidLoad```, this should do the trick for resetting the title every time you press the button. Regarding the storyboard, I don't use it much, so not sure with the problem, I am gonna make a test project and will let you know a lil later :] – Joke coder Nov 16 '21 at 05:59
  • Update: I just created a test project and was able to remove the button title from storyboard by just emptying the title section of the button. Don't really know why you are having issues with it. I would look into your project, if you could share the current or make a test project and replicate the issue :] – Joke coder Nov 16 '21 at 06:14
  • Thank you all for your replies! It now works fine, see below ... Eric thanks, I actually already tried to change it to Custom, but that didn't change anything. @Jokecoder, thanks, I also created a blank new project and there *it worked fine* indeed. I had already previously deleted the button and put it back with no luck, but this time I also made sure to remove the links in the Connection Inspector which I may have skipped the previous time. After doing so, I added the button back and now it just works fine. So there must have been some glitch that needed to be cleaned out. – rafi Nov 16 '21 at 13:54
  • I just wonder why most of you prefer to add UI elements progrmmatically and don't use the storyboard with outlets? Is it so that you have better control? Thanks! – rafi Nov 16 '21 at 13:55
  • @rafi imho storyboards are actually great, but some work places require to do everything programmatically for less merge conflicts, but again this can be easily fixed by working in different storyboards. The one thing I find a lil annoying in storyboard is localizations, but again you can localize storyboards as well, but it is not really comfortable, so for that you would end up IBoutleting your elements anyways :] – Joke coder Nov 16 '21 at 14:53
  • @jokecoder thanks! That's very useful info. – rafi Nov 16 '21 at 15:34

4 Answers4

65

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:-

enter image description here

Then change the button style property as Default and remove title text for the same like below:-

enter image description here

Ashutosh Mishra
  • 1,679
  • 1
  • 13
  • 16
  • 3
    This worked for me, thanks! Also -- you can highlight multiple buttons at the same time and then swap them all from Plain to Default, then erase the Title text for all of them. You can then swap all of them back to Plain at the same time to fix multiple buttons at the same time. – Mark Han Feb 08 '22 at 18:35
  • Right Mark. Agreed you can set multiple button state config on the basis of their style. – Ashutosh Mishra Feb 09 '22 at 06:47
  • same bug in xcode 13.3... – Michael42 Apr 11 '22 at 16:34
  • 1
    @Michael42, I hope the above solution works for the same as well. – Ashutosh Mishra Apr 13 '22 at 07:01
  • @AshutoshMishra, yes so far this solution works – Michael42 Apr 14 '22 at 07:36
  • one month later, none of solutions above doesn't work! weird xcode bug! – Michael42 May 24 '22 at 12:46
  • Yeah this was fixed and now not working – Droid Chris Jun 07 '22 at 03:39
  • For anyone else experiencing this issue, the above answer and the comments are correct, this used to be the method to get this to work; now, however, you must do the above and set the image as the "BACKGROUND" property instead of just the image properly. It's found directly below the normal image property in the inspector panel. – cdunn2013 Aug 09 '22 at 22:15
16

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.

wildcat12
  • 975
  • 6
  • 13
4

Change the UIButton style to "Default" and clear its title.

Abdul Karim Khan
  • 4,256
  • 1
  • 26
  • 30
  • 1
    Thanks. As you can see in my last comment to the original post, in my case, I had a link in the Connection Inspector that needed to be deleted. – rafi Apr 21 '22 at 08:28
0

I just fill the title with a blank character (using the space bar) and it is good...

ΩlostA
  • 2,501
  • 5
  • 27
  • 63