Skip to content

Instantly share code, notes, and snippets.

@dev-yong
Last active May 9, 2019 21:54
Show Gist options
  • Save dev-yong/c5024119f321148174055b96a40d90ca to your computer and use it in GitHub Desktop.
Save dev-yong/c5024119f321148174055b96a40d90ca to your computer and use it in GitHub Desktop.
public class DynamicHeightTableView: UITableView {
public var maxHeight: CGFloat = UIScreen.main.bounds.height
override public var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override public func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
override public var intrinsicContentSize: CGSize {
super.layoutIfNeeded()
let height = min(contentSize.height, maxHeight)
return CGSize(width: contentSize.width, height: height)
}
}
@dev-yong
Copy link
Author

dev-yong commented May 7, 2019

import UIKit

final class ContentSizedTableView: UITableView {
    override var contentSize:CGSize {
        didSet {
            invalidateIntrinsicContentSize()
        }
    }

    override var intrinsicContentSize: CGSize {
        layoutIfNeeded()
        return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment