How to increase the height of the parent view according to the height of the UITableView in swift?

I have below screen hierarchy.

-ScrollView 
 -ContentView
   -View1
   -View2
   -View3
   -Collectioview
     -CollectioviewCell 
       -Tableview 
         -Tableviewcell 
            -Content

Now here, my table content looks dynamic, so the height of the tableview cell cannot be static.So the height of the table will increase depending on the number of cells and the height of each cell.

Depending on the height of the table, the height of the collection view should increase, which leads to an increase in the viewing height of the scroll content.

But I can't figure out how to do this with less code? or can it be done programmatically?

enter image description here

+6
source share
5 answers

- contentSize scroll-/table-/collectionView .

, :

: UIScrollView, collectionView

  • scrollViewToObserveSizeOf.addObserver(observerObject, forKeyPath: "contentSize", options: [.old, .new], context: nil)
    
  • observeValue

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let scrollView = object as? UIScrollView,
            scrollView == scrollViewToObserveSizeOf,
            keyPath == "contentSize" {
            // Do whatever you want with size
            // You can get new size from scrollView.contentSize
            // or change?[NSKeyValueChangeKey.newKey] as? CGSize for new value
            // and change?[NSKeyValueChangeKey.oldKey] as? CGSize for old value
            //
            // ! Do not force unwrap this values, just never use force unwrap, 
            // everything could happen
        } else {
            // ! Do not forget to call super.observeValue(forKeyPath:of:change:context) 
            // if it is not object you added observer to
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    

, , UITableView, , observeValue UITableView.

, , , - .

+3

, .

, view * height of row. , 30, 4 ,

tableHeight = 4*30 = 120

, tableview . , tableview, . , . . , , tableview collectionview .

. ( ) .

: tableViewHieghtConstraint.constant = tableHeight collectionViewHieghtConstraint.constant = tableHeight

tableView , , .

, . , .

func reloadData() {
        self.tableView.reloadData()
        let tableHeight: CGFloat = 0
        DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
            for i in 0...aryData.count - 1 {
                let frame = self.tableView.rectForRow(at: IndexPath(row: i, section: 0))
                tableHeight += frame.size.height
                print(frame.size.height)
            }
        }
        tableViewHieghtConstraint.constant = tableHeight 
        collectionViewHieghtConstraint.constant = tableHeight
    }  

. , , .

P.S: contentview.

+1

:

.

, , .

0

UITableView intrinsicContentSize contentSize . , , invalidateIntrinsicContentSize()

YourCustomTableView: override var intrinsicContentSize: CGSize { return contentSize }

YourViewController , :

func refreshViewController() { // do some stuff with your custom table view yourCustomTableView.invalidateIntrinsicContentSize() }

0

, ,

-CollectionView 
  -CollectioviewCell
  -CollectioviewCell
  -CollectioviewCell
  -CollectioviewCell
    -Collectioview
      -CollectioviewCell 
        -Tableview 
          -Tableviewcell 
             -Content
0

All Articles