Search code examples
iosswiftxcode6.3today-extension

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

Solution

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