简体   繁体   中英

Popover presentation controller - view has spacing around, doesn't fit full width

I am having an issue while displaying UITableViewController as a popover. The popover appears but its width in not full screen - there is some kind of margin on leading and trailing side. Check screenshot for more info:

在此处输入图片说明

This is the code I am using to setup and present controller:

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

There is also implemented adaptivePresentationStyle function but still, no difference:

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

ADDITIONAL QUESTION

Is there any way top arrow can be changed / modified? This arrow seems a bit too big for my use so is there a way to make it smaller or just replace it with custom one?

EDIT

After some investigating and printing out controller.view.superview?.frame I noticed its width is 20 points smaller than it should be which explains that space on both sides of the popup. But what's the way to get rid of it?

You should try this code,

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
}

and for brief understanding, how i create and present a 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
}

To Present in a Specific 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
    }

In ViewDidLoad(), you can configure your tableView.

you can try this.

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

Using this property, you can apply a margin from the end of the screen. https://developer.apple.com/documentation/uikit/uipopovercontroller/1624657-popoverlayoutmargins?language=objc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM