弹出窗口控制器 - 视图有间距,不适合全宽

时间:2017-08-09 09:50:43

标签: ios swift uitableview uikit uipopovercontroller

我在将UITableViewController显示为弹出窗口时出现问题。弹出窗口但宽度不全屏 - 前端和后端有一些边距。查看截图以获取更多信息:

enter image description here

这是我用来设置和呈现控制器的代码:

let controller = UITableViewController()
      controller.preferredContentSize = CGSize(width: self.view.bounds.width, height: self.popOverPresentationControllerHeight)
      controller.modalPresentationStyle = .popover
      controller.tableView.separatorStyle = .none
      controller.tableView.allowsMultipleSelection = true
      controller.tableView.dataSource = self.filterOptionsDataSource
      controller.tableView.delegate = self
      controller.tableView.isScrollEnabled = false

      controller.tableView.register(FilterOptionCell.self, forCellReuseIdentifier: FilterOptionCell.identifier)
      controller.tableView.register(UINib(nibName: FilterOptionCell.identifier, bundle: nil), forCellReuseIdentifier: FilterOptionCell.identifier)
      controller.tableView.register(FilterConfirmCell.self, forCellReuseIdentifier: FilterConfirmCell.identifier)
      controller.tableView.register(UINib(nibName: FilterConfirmCell.identifier, bundle: nil), forCellReuseIdentifier: FilterConfirmCell.identifier)

      controller.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)

      controller.popoverPresentationController?.delegate = self
      self.present(controller, animated: true, completion: {
        UIView.animate(withDuration: 0.25) {
          controller.view.superview?.layer.cornerRadius = 4
          self.view.alpha = 0.4
        }
      })

      controller.popoverPresentationController?.backgroundColor = UIColor.white
      controller.popoverPresentationController?.sourceView = sender
      controller.popoverPresentationController?.sourceRect = sender.bounds

还实现了adaptivePresentationStyle功能,但仍然没有区别:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
  }

其他问题

有没有办法改变/修改顶部箭头?这个箭头看起来有点太大了,所以有没有办法让它变小或者只用自定义的替换它?

修改

经过一些调查和打印controller.view.superview?.frame后,我注意到它的宽度比它应该小20个点,这解释了弹出窗口两侧的空间。但是摆脱它的方法是什么?

2 个答案:

答案 0 :(得分:0)

你应该试试这段代码,

override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        var size = self.tableView.contentSize
        size.width = 150.0 //Here, you can set the width.
        self.preferredContentSize = size
}

以及简要理解,我如何创建和呈现popoverController。

@objc open static func instantiate() -> PopOverViewController {
        let storyboardsBundle = getStoryboardsBundle() //Or Bundle.main
        let storyboard:UIStoryboard = UIStoryboard(name: "PopOver", bundle: storyboardsBundle)
        let popOverViewController:PopOverViewController = storyboard.instantiateViewController(withIdentifier: "PopOverViewController") as! PopOverViewController

        popOverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
        popOverViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up

        // arrow color
        popOverViewController.popoverPresentationController?.backgroundColor = UIColor.darkGray


        return popOverViewController
}

要在特定的ViewController中呈现,

let popover = PopOverViewController.instantiate()
        popover.popoverPresentationController?.sourceView = self.underlyingTextView
        popover.popoverPresentationController?.sourceRect = rect
        popover.presentationController?.delegate = self
        viewController.present(popover, animated: true, completion: nil)

//MARK: UIAdaptivePresentationControllerDelegate
    public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }

    public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }

在ViewDidLoad()中,您可以配置tableView。

答案 1 :(得分:0)

您可以尝试这个。

controller.popoverLayoutMargins = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)

使用此属性,您可以从屏幕末端开始添加边距。 https://developer.apple.com/documentation/uikit/uipopovercontroller/1624657-popoverlayoutmargins?language=objc