George M Ceaser Jr George M Ceaser Jr - 6 months ago 98
Swift Question

Collapse hidden UIButton

In my code I hide various UIButtons on my page using UIButton.hidden if the data source for that button does not contain a value.

For example a business may or may not have a URL. In my app, if a business has a website, I load it into the title of a button so the use can click on the button and be taken to the business' website. If the business does not have a website, I hide the button.

The challenge is that the button appears to take up screen real-estate even when it is hidden. I have even tried the following code and the button still consumes space on the page:

btnURL.frame.size.height = 0
btnURL.frame.size.width = 0
btnURL.setTitle("", forState: .Normal)
btnURL.hidden = true

Any suggestions on how to get the button to not consume space on the screen? I don't want to remove it because there are several other buttons and labels that have constraints dependent on this button.


I had the same problem, I add button width constrain and set it to zero:

btnURLWidth.constant = 0

P.S. To hide label you can set zero font.

[UIFont systemFontOfSize:0]