如何将弹出窗口调整为 swift 中 tableview 中内容的大小?

How do I adjust my popover to the size of the content in my tableview in swift?

我正在使用 popoverPresentationController 来显示我的弹出窗口。用于显示为弹出窗口的 UITableViewController 以编程方式创建,通常包含 1 到 5 行。如何设置此弹出窗口以根据 tableview 的内容调整大小?

我的弹出框代码:

if recognizer.state == .Began {
    let translation = recognizer.locationInView(view)

    // Create popoverViewController
    var popoverViewController = UITableViewController()
    popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
    popoverViewController.tableView.backgroundColor = UIColor.popupColor()

    // Settings for the popover
    let popover = popoverViewController.popoverPresentationController!
    popover.delegate = self
    popover.sourceView = self.view
    popover.sourceRect = CGRect(x: translation.x, y: translation.y, width: 0, height: 0)
    popover.backgroundColor = UIColor.popupColor()

    presentViewController(popoverViewController, animated: true, completion: nil)
}

检查preferredContentSize property of UIViewController

let height = yourDataArray.count * Int(popOverViewController.tableView.rowHeight)
popOverViewController.preferredContentSize = CGSize(width: 300, height: height)

uitableviewcontroller 的扩展中覆盖 preferredContentSize 属性,如下所示:

override var preferredContentSize: CGSize {
    get {
        let height = calculate the height here....
        return CGSize(width: super.preferredContentSize.width, height: height)
    }
    set { super.preferredContentSize = newValue }
}

计算身高请查看tableView.rectForSection(<#section: Int#>)

第一件事:所有评论都很好,帮助满满。 我几乎没有改变我的逻辑,这使我的 VC 成为可重用的组件。

viewWillAppear:(BOOL)animated中调用此方法:

-(void) setPopOverPreferedContentHeight {

     if (self.popoverPresentationController && self.tableView.contentSize.height < MAX_POPOVER_HEIGHT) {
          self.preferredContentSize=self.tableView.contentSize;
     } else if (self.popoverPresentationController){
         self.preferredContentSize = CGSizeMake(self.tableView.contentSize.width, MAX_POPOVER_HEIGHT);
   }
}

在您的 UITableViewController 的 viewDidLoad() 中,您可以添加一个观察者:

self.tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)

然后添加这个方法:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    self.preferredContentSize = tableView.contentSize
}

最后,在 viewDidDisappear() 中,确保移除观察者:

tableView.removeObserver(self, forKeyPath: "contentSize")

这样,无论何时加载或更改,弹出窗口都会自动调整大小以适合内容。

For swift 4 如果您想观察内容大小,我发现这是最佳解决方案。网上没找到完整的例子报告一下:

class MyTableViewController: UITableViewController {

    private var kvoContext = 0

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        addObserver(self, forKeyPath: #keyPath(tableView.contentSize), options: .new, context: &kvoContext)

    }

    override func viewDidDisappear(_ animated: Bool) {
        removeObserver(self, forKeyPath: #keyPath(tableView.contentSize))
        super.viewDidDisappear(animated)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if context == &kvoContext, keyPath == #keyPath(tableView.contentSize),
            let contentSize = change?[NSKeyValueChangeKey.newKey] as? CGSize  {
            self.popoverPresentationController?.presentedViewController.preferredContentSize = contentSize
        }
    }
}

Swift 4.xSwift 5.x 的简单动态答案不涉及尺寸计算(Bo Frese 答案的现代版本):

private var contentSizeObserver : NSKeyValueObservation?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    contentSizeObserver = tableView.observe(\.contentSize) { [weak self] tableView, _ in
        self?.preferredContentSize = CGSize(width: 320, height: tableView.contentSize.height) // Here I fixed the width but you can do whatever you want
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    contentSizeObserver?.invalidate()
    contentSizeObserver = nil
}