Dynamically change the size of a container view in swift

845 views Asked by At

As the title suggest, I am trying to change the size of a container view that resides within a table view controller. The following code calculates the height that the container view will have to become.

Basically it will take the height of the users phone subtracted by the last cell's Y-AXIS in the table view.

I have attempted to use containerView.bounds.maxY and set its value, however xcode gives me an error saying that it is only a get property. So i was wondering how someone would go about doing this? There has to be a function I can override before the container view gets displayed with my custom calculated height.

var dynamicCotainerViewHeight: CGFloat?
@IBOutlet weak var containerView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    // call somesort of method that will be called before the container view gets displayed
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if cell.reuseIdentifier == "lastStaticCell" {
            let rectOfCellInTableView = tableView.rectForRow(at: indexPath)
            let rectOfCellInSuperview = tableView.convert(rectOfCellInTableView, to: tableView.superview)

            let screenheight = self.view.frame.size.height

            // Get the desired height for the container view
            dynamicCotainerViewHeight = CGFloat(screenheight) - CGFloat(rectOfCellInSuperview.origin.y)
            print("Height of dynamic\(dynamicallyAllocatedCellHeight)")
        }
    }

This is what I have so far in the storyboard to give a better understanding of what I mean. Since I cannot seem to constrain the container view to its superviews bounds I am stuck with this problem

enter image description here

0

There are 0 answers