Yosi199
Yosi199

Reputation: 1804

Resize ScrollView based on TableView size

I'm trying to create layout that it structured like this:

 - View 
   -- ScrollView
       --- ContentView
            ---- CustomView
            ---- CustomView
            ---- TableView
            ---- CustomView

The tableView itself is auto-resizable using "invalidateIntrinsicContentSize" and when I add items - the height of the tableview changes, pushing the custom view below it further down.

Once enough items are added I the bottom custom view is hidden and the scroll doesn't work.

important fact - the bottom custom view doesn't have a bottom constraint. It is pushed down by the it's top constraint to the tableView.

If I do set a bottom constraint - the table view will no longer be dynamically resized.

The intended behaviour: When a user adds items to the list and the list gets too big the ContentView will be scrollable so the user can scroll to see the bottom view.

The actual behaviour: When a user adds items to the list and the list gets too big, the bottom view is pushed down and outside of sight and content is not scrollable.

What is happening and how can I fix it?

Upvotes: 0

Views: 798

Answers (4)

vdmzz
vdmzz

Reputation: 261

What I think you could do is:

  1. Disable tableView's scroll tableView.isScrollEnabled = false
  2. Every time a user adds items to the list, reload the tableView

Also using UIStackView with vertical axis and .fillEqually distribution as a Content View would be much more convenient as you won't need to set any positional constraints to your views, but may need to set height constraints if intrinsic content size can't be determined by the engine

Upvotes: 0

kerry
kerry

Reputation: 2582

Below is what I think what is happening.

Since you are using UITableView, it has its own scroll view. So when the UITableView list gets too big, UITableView itself becomes scrollable rather than ScrollView's contentView becoming scrollable.

To achieve what you need, you would have to make the UITableView not scrollable and use the intrinsicHeight of the UITableView to get the actual height of UITableView along with all the items. If you have items with varying heights, it will be a problem because you won't know the height before rendering. With same height for all the rows, you can get the total height of the UITableView and set the height constraint to that value. This will increase the contentSize of the outer ScrollView, making it scrollable.

Apart from UITableView, you can also use UIStackView. This is because you are not using the reusing capabilities of UITableView anyways. Managing the datasource and delegates should not be a big problem.

Upvotes: 1

Jins George
Jins George

Reputation: 121

try this code

        tblViewHeight.constant = CGFloat( tableview row count * 45 ) 
        var size = contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
        if size.height < scrollView.frame.size.height
         {
            size = scrollView.frame.size
        }
        contenViewHeight.constant = size.height - scrollView.frame.size.height
        scrollView.contentSize.height = contenViewHeight.constant

Upvotes: 0

Asad Ali Choudhry
Asad Ali Choudhry

Reputation: 5271

You can create a constraint for tableview height, And take its reference to your swift file, by dragging it as you take other views. Now in your code, Just do this

tableViewHeightConstraint.constant = tableViewNoOfItems * tableViewCellHeight;

if you have set other constraints perfectly inside scrollview, It should work perfectly. Means TableView should have top, bottom, left, right margined constraints from the ScrollView.

Upvotes: 1

Related Questions