TableView created by view code is not scrolling till the end of the content

I'm trying to create a view that have a horizontal stackView with 2 buttons on the first line and a table on the second line. This tables can increase its height and my problem is when the table height is too high the buttons are moved to outside the screen on the top and I can't scroll the table to it's bottom. How can I set the constraints properly to make this behavior happen? when the table height is too high it should only create a scroll and not move the other components. Could you guys help me?

Here I update the table height.

   func updateTableHeight() {
        UIView.animate(withDuration: 0, animations: {
            self.tableView.layoutIfNeeded()
        }) { (complete) in
            self.heightOfTableViewConstraint?.constant = self.tableView.contentSize.height
        }
    } 

Here I'm setting up all the constraints:

private func setUpViewConstraints() {
        tableView.translatesAutoresizingMaskIntoConstraints = false
        goToNextPageButton.translatesAutoresizingMaskIntoConstraints = false
        goToPreviousPageButton.translatesAutoresizingMaskIntoConstraints = false
        buttonsStackView.translatesAutoresizingMaskIntoConstraints = false
        mainStackView.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .black
        let viewsDictionary: [String: Any] = ["tableView": tableView,
                                              "goToNextPageButton": goToNextPageButton,
                                              "goToPreviousPageButton": goToPreviousPageButton,
                                              "mainStackView": mainStackView,
                                              "buttonsStackView": buttonsStackView]

        let mainStackViewHorizontalConstraints =
            NSLayoutConstraint.constraints(withVisualFormat: "H:|-40-[mainStackView]-40-|",
                                           options: NSLayoutFormatOptions(rawValue: 0),
                                           metrics: nil, views: viewsDictionary)
        let mainStackViewVerticalConstraints =
            NSLayoutConstraint.constraints(withVisualFormat: "V:[mainStackView]",
                                           options: NSLayoutFormatOptions(rawValue: 0),
                                           metrics: nil, views: viewsDictionary)

        let buttonsStackViewVerticalConstraints =
            NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[buttonsStackView]",
                                           options: NSLayoutFormatOptions(rawValue: 0),
                                           metrics: nil, views: viewsDictionary)

        let buttonsStackViewHorizontalConstraints =
            NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[buttonsStackView]-0-|",
                                           options: NSLayoutFormatOptions(rawValue: 0),
                                           metrics: nil, views: viewsDictionary)

        let tableViewViewHorizontalConstraints =
            NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[tableView]-0-|",
                                           options: NSLayoutFormatOptions(rawValue: 0),
                                           metrics: nil, views: viewsDictionary)

        buttonsStackView.addConstraint(NSLayoutConstraint(item: goToPreviousPageButton,
                                              attribute: .height,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .height,
                                              multiplier: 1,
                                              constant: 80))

        buttonsStackView.addConstraint(NSLayoutConstraint(item: goToNextPageButton,
                                              attribute: .height,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .height,
                                              multiplier: 1,
                                              constant: 80))

        view.addConstraint(NSLayoutConstraint(item: mainStackView,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: view,
                                              attribute: .top,
                                              multiplier: 1,
                                              constant: 40))

        view.addConstraints(mainStackViewHorizontalConstraints)
        view.addConstraints(mainStackViewVerticalConstraints)
        view.addConstraints(buttonsStackViewHorizontalConstraints)
        view.addConstraints(buttonsStackViewVerticalConstraints)
        mainStackView.addConstraints(tableViewViewHorizontalConstraints)
    }