uitableviewswift3contentsize

How to get content size of UITableView in runtime?


I'm trying to get the content size of UITableView in runtime, where I working on library that will do some moves when pop over the views and to do this I wrote this code inside the library to figure out that inside the presented view will be UITableView:

if view.isKind(of: UIView.self) {
            for subView in view.subviews {
                for sv in subView.subviews {
                    if sv.isKind(of: UITableView.self) {
                        print(sv.frame.size.height)

Here I got the table view, but I couldn't get the contentSize property

                        print("table view found")
                    }
                }
            }
        }

Any tips how to get its content size ?!


Solution

  • Use a cast instead of a test so that the compiler knows the object's properties:

    if view.isKind(of: UIView.self) {
        for subView in view.subviews {
            for sv in subView.subviews {
                if let tableView = sv as? UITableView {
                    print(tableView.contentSize.height)
                }
            }
        }
    }