-2

I'm using Snapkit in my iOS project.

View -> ScrollView -> Tableview

I tried view.addSubview(scrollView) scrollView.addSubview(tableView)

I tried setting a bunch of constraints on the tableview but it never shows up on the scrollview. Whereas if I add it to the view (view.addSubview(tableView) ) then it shows up correctly.

Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64
  • what's your purpose when adding tableView as subview of a UIScrollView? – nghiahoang Jun 25 '20 at 10:58
  • Need to display a list of items surrounded by few labels, buttons, etc – Bilbo Baggins Jun 25 '20 at 11:01
  • what is your size of table you want? what is scroll direction of scrollView (horizontal, vertical)? – nghiahoang Jun 25 '20 at 11:11
  • table width = 100%, height = as much required. scroll direction = vertical – Bilbo Baggins Jun 25 '20 at 11:13
  • I don't understand why you have to use an additional uiScrollView with these requirements, however you can check the answer – nghiahoang Jun 25 '20 at 11:19
  • I would recommend using the table view header and footer instead of embedding it in a scroll view, but if you still want to do that you could use the `StiffTableView` class I talk about on [this article](https://savvyapps.com/blog/using-advanced-auto-layout-techniques-to-adapt-interfaces-to-screen-content-intrinsic-content-size-stack-views) – EmilioPelaez Jun 25 '20 at 11:28

1 Answers1

0
    label.text = "hello world"
    scrollView.addSubview(label)
    scrollView.addSubview(tableView)
    view.addSubview(scrollView)
    
    scrollView.snp.makeConstraints { (make) in
        make.edges.equalToSuperview()
        make.width.equalToSuperview()
    }

    label.snp.makeConstraints { make in
        make.top.left.right.equalToSuperview()
        make.bottom.equalTo(tableView.snp.top)
    }
    
    tableView.snp.makeConstraints { (make) in
        make.left.right.bottom.equalToSuperview()
        make.width.height.equalTo(view)
    }
    
nghiahoang
  • 538
  • 4
  • 10