Search code examples
swiftuiscrollviewconstraintstableviewuistackview

how can I make the the view show all my table view data while having it inside a view inside a scroll View


I have scroll view inside it I have a stack view inside it there is multiple views and a table view. when I run my app the table view can't be shown completely like only the first few rows can be shown like shown in the below photos:

also if I try to give constraints to table view like a height constraint it well be too big or too small depending on how much data am showing in the table view.

so the current constraints are: 128 for the tableView 1500 for the stackView if I run the app this is the result like shown in picture #1 picture #1

if I remove the height constraint for the table view and keep the the 1500 stackView constraint the orange view will completely disappear for some reason like in picture #2

picture #2

if I remove the height constraint for the stack view and keep the 128 height constraint for the table view the orange view will also disappear but the whole screen will shrink as the orange view doesn't exist for some reason like in picture #3 picture #3

and lastly if I remove both the height constraint for the stack view and the height constraint for the table view the orange view and the table view will disappear this time and the whole screen will shrink as the orange view and table didn't exist like in picture #4 picture #4

I am guessing am doing something very silly for this to happen and its solution is much simpler than what i think it is some screen shots

am also thinking the way to make this work is keeping the Constraints for both the stack view and table View and programmatically find a way to add more or less height for the tableView depending on how much data and rows I have to display

am not sure if this is even possible

please if anyone has any clue any help will be appreciated thanks all in advance


Solution

  • so I just answered my question it was simpler than I expected

    I connected an iboutlet to height constraint of tableView and change it's value programmatically : and just added this line of code

    heightConstraint.constant = CGFloat(myArray.count * 62) // 62 is the height of the each row in the tableView