如何将UIViewController的preferredContentSize设置为包含UITableView contentSize的高度?

时间:2017-05-19 07:58:56

标签: swift uitableview

我打电话给下面的弹出窗口:

if let vc = CalendarDetailViewControllerContainer.storyboardInstance() {
    vc.preferredContentSize = CGSize(width: ((self.view.frame.width / 5) * 2), height: vc.view.frame.height);
    vc.modalPresentationStyle = UIModalPresentationStyle.popover;
    vc.popoverPresentationController?.delegate = self;
    vc.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.any;
    vc.popoverPresentationController?.sourceView = sourceView;
    vc.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: sourceView.frame.size.width, height: sourceView.frame.size.height);

    self.present(vc, animated: true, completion: nil);
}

正如您现在所看到的,我将preferredContentSize高度设置为vc.view.frame.height。但那应该是不同的。现在,更多的代码。在下面我的CalendarDetailViewControllerContainer

class CalendarDetailViewControllerContainer : UIViewController {

    @IBOutlet weak var containerView: UIView!;

    static func storyboardInstance() -> CalendarDetailViewControllerContainer? {
        let storyboard = UIStoryboard(name: String(describing: NSStringFromClass(CalendarDetailViewControllerContainer.classForCoder()).components(separatedBy: ".").last!), bundle: nil);

        return storyboard.instantiateInitialViewController() as? CalendarDetailViewControllerContainer;
    }

    override func viewDidLoad() {
        super.viewDidLoad();

        self.setupView();
        self.setupDetailView();
    }

    func setupView() {
        self.view.backgroundColor = UIColor.clear;
        self.view.isOpaque = false;
    }

    func setupDetailView() {
        if let vc = CalendarDetailViewController.storyboardInstance() {
            let navigationController = UINavigationController(rootViewController: vc);

            self.addChildViewController(navigationController);

            navigationController.view.frame = self.containerView.frame;

            self.containerView.addSubview(navigationController.view);

            navigationController.didMove(toParentViewController: self);
        }
    }
}

我需要这个容器,因为为了以后的目的和将来的segue我需要导航控制器(我不会在故事板中构建它)。所以代码的最后一部分:

class CalendarDetailViewController : UITableViewController {

    static func storyboardInstance() -> CalendarDetailViewController? {
        let storyboard = UIStoryboard(name: String(describing: NSStringFromClass(CalendarDetailViewController.classForCoder()).components(separatedBy: ".").last!), bundle: nil);

        return storyboard.instantiateInitialViewController() as? CalendarDetailViewController;
    }

    override func viewDidLoad() {
        self.navigationController?.isNavigationBarHidden = true;

        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 120.0;
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude;
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude;
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;
    }
}

我现在要做的是将弹出窗口的preferredContentSize高度设置为表格视图的contentSize

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以为CalendarDetailViewControllerContainer覆盖preferredContentSize,但是在计算大小之前,请确保重新加载表并调用layoutIfNeeded()

public override var preferredContentSize: CGSize {
    get {
        return CGSize(width: tableView.contentSize.width,
                      height: tableView.contentSize.height + tableView.layoutMargins.top + tableView.layoutMargins.bottom)
    }

    set { super.preferredContentSize = newValue }
}