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)
}
you can achieve that by using tableView.contentSize
this will tell you the total size of the tables content.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With