Xcode IB: UIButton hidden but have still buttons space

后端 未结 6 1774
悲哀的现实
悲哀的现实 2021-02-15 18:22

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

相关标签:
6条回答
  • 2021-02-15 18:38

    maybe there is a solution with autoLayout in IB but I'm not sure about that. Programmatically you can add all your button to an array in order. and whenever you hide a button or not you loop trough the array of button and each time you find one that is not hidden you set the y coordinate on the frame to a a value and you increment this value by what you need so the next not hidden will be placed according to the last position used.

    0 讨论(0)
  • 2021-02-15 18:43

    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.

    0 讨论(0)
  • 2021-02-15 18:45

    You had just make it hidden. You have to set the frames according to your need. OR You can set autolayout.

    0 讨论(0)
  • 2021-02-15 18:47

    The modern preferred way of doing so is to use Stack Views. Great tutorial. Requires iOS 9.

    You'll find the icon of stack view in the Auto Layout toolbar at the bottom right of the storyboard canvas.

    0 讨论(0)
  • 2021-02-15 18:49

    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.

    0 讨论(0)
  • 2021-02-15 18:55
    1. You can add the buttons programmatically -> you will have the array of Btns and method to add array to the view controller.
    2. You can play with the Constraints and set Height Constraint for 2nd button when hide it to 0 but all buttons should be connected with the constraints in this case.
    0 讨论(0)
提交回复
热议问题