Reputation: 237
I implemented an table view into my today widget. The tableView has no fixed number of cells. Because of this the height of the widget has to change dynamically. Does someone knows how I can find out the height of the tableView? If I know the height of the tableView I can use the same height for the today widget. Or is there a better solution?
My code:
var preferredViewHeight:CGFloat{return 132}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
updateSize()
}
func updateSize() {
var preferredSize = self.preferredContentSize
preferredSize.height = self.preferredViewHeight
self.preferredContentSize = preferredSize
}
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
self.tableView.reloadData()
completionHandler(NCUpdateResult.NewData)
}
Upvotes: 5
Views: 1052
Reputation: 321
As answered above, you would implement it as:
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
switch activeDisplayMode {
case .compact:
preferredContentSize = CGSize(width: maxSize.width, height: 300)
case .expanded:
preferredContentSize = tableView.contentSize
}
}
Upvotes: 1
Reputation: 2343
you can achieve that by using tableView.contentSize
this will tell you the total size of the tables content.
Upvotes: 5