Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic resizing today widget

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)
}
like image 928
paro Avatar asked Aug 12 '15 12:08

paro


2 Answers

you can achieve that by using tableView.contentSize this will tell you the total size of the tables content.

like image 88
Or Ron Avatar answered Nov 15 '22 05:11

Or Ron


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
    }
}
like image 21
Minhal Khan Avatar answered Nov 15 '22 04:11

Minhal Khan