1

I stand in front of a new problem for me. I have the following layout in my storyboard.

layout

In this Layout you see a scrollView that have always the same content on top and some boxes at the bottom. These boxes should be variable, so I want some of them turned off and some turned on. I already tried to say something like mapBox.hidden = true or mapBox.removeFromSuperview().

The problem with that is if I do that the box is hidden, but the space of it is always there, because of the constraints I think.

I already done it on Android, there you can give them boxes just a margin and when you make dem hidden the following box have just its margin to whatever was on top of the hidden box.

What I want is the possibility to turn some of the boxes on and off with always the same distance between all not hidden boxes and a scrollview thats just big as its content.

I hope you can read my maybe unintelligible english and can give me a hint.

jmq
  • 10,110
  • 16
  • 58
  • 71
kuemme01
  • 472
  • 5
  • 19

2 Answers2

0

What you could do here instead: Make the whole layout a table view. Then, the top part (that always stays the same) could be added as the table header. The mapbox and headline boxes would both be table cells (use custom XIB files). Then, you can have one array that represents which boxes, along with which other cells you would like to display, and update that array depending on the situation. A Table view will adjust the size of its scroll view based on the size of content.

Ian Richard
  • 525
  • 4
  • 9
  • 1
    Yeah, nice Idea. But I have some other Views and Buttons all over the Layout I haven't shown on my Layoutpicture to make it more easy for you to understand. I think it becomes more complicated when I try to make a tableView out of it. – kuemme01 Aug 17 '16 at 16:22
0

You probably know that the big issue here is the basic design. As @IanRichard said, this should be done using a table view. However you still can make some workarounds if there's no other way out.

You can set the constraints in such way so that an "undesired" view gets hidden. Here's an example for textBox and mapBox, but can use the same for all boxes:

You probably have one constraint connecting the bottom of the textBox and the top of the mapBox, and the priority is probably 1000. Change this priority to 999. Then add one more constraint connecting the TOP of the textBox and TOP of the mapBox. Set the priority to 1 and constant to 0. Next, in the same way as you were making outlet connections for other view elements, make two more outlet connections for this both constraints. You'll need them later when you will switch the priorities.

Here's how your outlets should look like:

IBOutlet var myConstraintBotTop  : NSLayoutConstraint!
IBOutlet var myConstraintTopTop  : NSLayoutConstraint!

Then in your code you can "turn your views off" by just setting the appropriate constraints:

func turnOffMyBox() {
    myConstraintBotTop.priority = 1
    myConstraintTopTop.priority = 999
}

And here's the inverted version for "turn on" in case you need it:

func turnOffMyBox() {
    myConstraintBotTop.priority = 999
    myConstraintTopTop.priority = 1
}

Note: this approach works by just overlapping your boxes. Those boxes still exists. You may want to hide them to prevent from being seen by the user.

Andrej
  • 7,266
  • 4
  • 38
  • 57
  • Hi Guys, sorry for my late respond. I think you right, i transformed my layout to one with a tableView. For that I used the technic shown in this tutorial: https://www.raywenderlich.com/129059/self-sizing-table-view-cells and from http://stackoverflow.com/questions/30774671/uitableview-with-more-than-one-custom-cells-with-swift to get different cells. Sadly without any success. My rows are always empty. – kuemme01 Aug 20 '16 at 09:25