Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode IB: UIButton hidden but have still buttons space

I have the following issue in my iPhone app. I have 4 buttons in my IB also linked to my UIViewController (IBOutlet) When I for example hide the second button which is AfvalSoorten with [self.btnAfvalSoorten setHidden:YES]; then it disappears, that's what I want, but I still got the button space when I debug the app on the simulator.

How can I get rid of that? Below there is an example.

enter image description here

enter image description here

Is there an option on the Storyboard for the buttons to clip together?

like image 509
Stefan van de Laarschot Avatar asked Oct 10 '14 12:10

Stefan van de Laarschot


People also ask

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again.

How do I change the size of a button in Xcode?

You can customize width and height of button using storyboard in Xcode. Just select your button and open this window. After that just add the height constraints.

What is a UIButton?

A control that executes your custom code in response to user interactions.


2 Answers

You should use autolayout. Otherwise it's a nightmare with the new screen sizes.

With autolayout you can do what you ask programmatically: setup the buttons with certain constraints and then when you decide to hide the button remove the constraints that are not needed. It's flexible and powerful but not the easiest way for a beginner.

One simple way to do it is with additional constraints. For instance, if you have buttons 1, 2 and 3 (see screenshot), and you plan to remove button 2, you can add an extra constraint between 3 and 1:

enter image description here

That constraint should have less priority (250 in my example) than the others (1000 by default). That mean that the constraint won't be applied when button 2 is in place (with higher priority constraints).

Then, remove the button instead of hiding it.

[self.button removeFromSuperview];

When you hide the button it still considered by the layout system to take decisions, and it makes layout more complex. If you want to keep the button around make sure it's using strong modifier in the property declaration.

like image 188
djromero Avatar answered Sep 30 '22 16:09

djromero


A better approach for above scenario - You dont need to set any autolayout or frames :)

Use UITableView and create custom cell with UIButtons in it.

  1. Set UITableViewCellSelectionStyle to None

  2. Here your button background is same for all cell

  3. Create an array with above button titles

  4. When ever you want to hide buttons just remove it from array.

like image 43
Kampai Avatar answered Sep 30 '22 14:09

Kampai