paro
paro

Reputation: 237

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)
}

Upvotes: 5

Views: 1052

Answers (2)

Minhal Khan
Minhal Khan

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

Or Ron
Or Ron

Reputation: 2343

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

Upvotes: 5

Related Questions