Reputation: 1217
I'm a swift beginner and I have come across the following issue.
I am displaying a UITableView within a UIView container just fine.
The issue is that the table will NOT scroll up/down.
I have read threads on a slightly similar issue and it points to the full table being displayed correctly but it is being clipped by the size of the UIView container, therefore making it not needing to be scrolled as all the cells are being showed.
So my question is, how do I resize the height of my table to be within the bounds of the View container so that it becomes scrollable?
This is my View Controller code:
I declare an outlet to the UIView here
@IBOutlet weak var viewOrdersDrinks: UIView!
Then I call this function in my viewDidLoad which loads the UITableView into the UIView as a subview.
func loadOrdersDrinks(){
let listDrinks: CompletedOrderedDrinksListTableViewController = CompletedOrderedDrinksListTableViewController (nibName: "CompletedOrderedDrinksListTableViewController", bundle: nil)
self.addChildViewController(listDrinks)
self.viewOrdersDrinks.addSubview(listDrinks.view)
}
Upvotes: 0
Views: 1310
Reputation: 4159
First of all you have to autolayout the tableView to fill the parent and here is an UIView extension for this:
extension UIView {
func anchorToSuperview() {
guard let superview = self.superview else { return }
self.translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true
}
}
Another thing to mention is that besides adding the tableViewController
as a child you have to respect other API calls, related to adding child view controllers. Here is the reference to the documentation.
All in all, for adding the tableView properly, your code in the end should look something like this:
func loadOrdersDrinks() {
let listDrinks: CompletedOrderedDrinksListTableViewController = CompletedOrderedDrinksListTableViewController (nibName: "CompletedOrderedDrinksListTableViewController", bundle: nil)
self.addChildViewController(listDrinks)
self.viewOrdersDrinks.addSubview(listDrinks.tableView)
self.listDrinks.tableView.anchorToSuperview()
self.listDrinks.didMove(toParentViewController: self)
}
Upvotes: 2