0
votes

I am working on a screen for an app using storyboard.

I have placed a UIScrollView and within this a UIVIew. The idea is for the screen to have the following items (starting from the top) inside the UIVIew:

  • An image view
  • A title label (single line)
  • A description label (multiple lines, dynamic)
  • A table view (dynamic)

I have set the following constraints:

  • Scroll View: 0 top, 0 leading, 0 trailing, 0 bottom
  • UIView: 0 top, 0 leading, 0 trailing, 0 bottom, equal widths with view controller

Then within this view:

  • Image View: 0 top, 0 leading, 0 trailing, aspect ratio
  • Title: 8 top, 16 trailing, 16 leading
  • Description: 8 top, 16 trailing, 16 leading
  • Table View: 20 top, 0 leading, 0 trailing, 20 bottom

I have tried similar approaches in other screens but only with labels and views and there haven't been any problems. However here it's expecting me to give the table view a height which I can't because the number of rows to be loaded is unknown (if I set a height all auto layout issues go away).

How can I constrain the Table View in relation to the other views?

I am working on XCode 10 for iOS 11, with Swift 4.2.

1
Why does the number of rows affect your ability to give the table view a height? The number of rows is irrelevant to setting the height of the table view. Set the table view's height to whatever you want. - rmaddy
Why you don't put the image, the title label and the description label in an own prototype cell? Then you don't need a scroll view. - Retterdesdialogs
he may need to stretch the table to full height according to it's size - Sh_Khan
@Retterdesdialogs that was my first choice, the problem is this screen appears with a modal segue and I need to include a close button on the top corner which remains fixed as the table scrolls. Every time I have tried to do this in a table view controller the close button moves with the rest of the table. - cesarcarlos
You may fix position by implementing func scrollViewDidScroll(_ scrollView: UIScrollView) or use a UIViewController - Sh_Khan

1 Answers

0
votes

Give it any height then hook it as outlet then

self.tableHeight.constant = tableView.contentSize.height
self.view.layoutIfNeeded()