Shadi Hammoudeh
Shadi Hammoudeh

Reputation: 101

Resizing the size of the popover ViewController to fit the data to be displayed in a UITableView

I am developing an application whereby I have two View Controllers. The first one is displaying a popover ViewController (for the second View Controller) when the user perform a long press gesture on a TextField. Below is the code the popover (in the first view controller) which is done programmatically:

        func longpressGestureTriggeredForIndicativeDesignWorkingLifeTextView (gestureRecognizer: UIGestureRecognizer) {

        if gestureRecognizer.state == UIGestureRecognizerState.began {

        self.view.endEditing(true)

       }

   let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "IndicativeDesignWorkingLifeVC")

   popController.modalPresentationStyle = UIModalPresentationStyle.popover
                popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up

   popController.popoverPresentationController?.delegate = self

   popController.popoverPresentationController?.sourceView = (indicativeDesignWorkingLifeTextView!)

   popController.popoverPresentationController?.sourceRect = (indicativeDesignWorkingLifeTextView as AnyObject).bounds

   self.present(popController, animated: true, completion: nil)

        }

When the second view controller pops over displaying the UITableView which it contain. The size of the popover VIewController is much larger than the number of rows contains in the table set up in the second View Controller.

How can I set the size of the popover ViewController to be equal in terms of height and width to the amount of data contained in the UITableView set in the second View Controller?

Upvotes: 0

Views: 966

Answers (1)

OverD
OverD

Reputation: 2632

In the viewWillAppear of the 2nd View controller you need to get the row height of the tableView cell and count the number of cells to get the total height of the tableview. Once you have done that you can change the height of the popover by using self.preferredContentSize

Upvotes: 1

Related Questions