Search code examples
swiftuitableviewstoryboard

Change position on elements inside a tableViewCell


So I have a view inside an xib cell, inside this view I have 2 elements, one label that is in the middle of the view and a button with text that is hidden. When this button goes from isHidden to isNot I want the label to be 20 px from the top of the view and the button 20px from the bottom of the view.

Could anyone go through this with me like u explained it to a complete beginner? I'm not that far into swift programming, and this is the first time I'm trying to do something like this.

I will give you an example in pictures what I'm trying to get as well.

buttonisHidden

buttonisNotHidden


Solution

  • I suggest using a vertical stack view as your containing view. Set up an AutoLayout constraint to center your stack view in your cell.

    A stack view is smart enough to adjust its size and layout when you show/hide the views inside it. (hiding a view in a stack view has the same effect as removing it, and un-hiding a child view has the same effect as adding it.)

    If you set up everything correctly, you should be able to just show/hide your button and the layout should just work.