Skip to content

Instantly share code, notes, and snippets.

@db42
Created March 2, 2018 02:24
Show Gist options
  • Save db42/72ad87c8a165e57f0db675a9fd3e5942 to your computer and use it in GitHub Desktop.
Save db42/72ad87c8a165e57f0db675a9fd3e5942 to your computer and use it in GitHub Desktop.
Swift 4 recipe: Self sizing table view
//
// SelfSizedTableView.swift
// AdjustableTableView
//
// Created by Dushyant Bansal on 25/02/18.
// Copyright © 2018 db42.in. All rights reserved.
//
import UIKit
class SelfSizedTableView: UITableView {
var maxHeight: CGFloat = UIScreen.main.bounds.size.height
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
self.layoutIfNeeded()
}
override var intrinsicContentSize: CGSize {
let height = min(contentSize.height, maxHeight)
return CGSize(width: contentSize.width, height: height)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment