Popover 演示控制器 - 视图周围有间距,不适合全宽
Posted
技术标签:
【中文标题】Popover 演示控制器 - 视图周围有间距,不适合全宽【英文标题】:Popover presentation controller - view has spacing around, doesn't fit full width 【发布时间】:2017-08-09 09:50:43 【问题描述】:我在将 UITableViewController 显示为弹出框时遇到问题。弹出窗口出现但它的宽度不是全屏 - 前导和尾随边有某种边距。查看屏幕截图了解更多信息:
这是我用来设置和展示控制器的代码:
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 磅,这解释了弹出窗口两侧的空间。但是有什么办法可以摆脱呢?
【问题讨论】:
【参考方案1】:你应该试试这段代码,
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。
【讨论】:
【参考方案2】:你可以试试这个。
controller.popoverLayoutMargins = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1)
使用此属性,您可以从屏幕末端应用边距。 https://developer.apple.com/documentation/uikit/uipopovercontroller/1624657-popoverlayoutmargins?language=objc
【讨论】:
请在您的答案中添加一些解释,以便其他人可以从中学习 使用此属性,您可以从屏幕末端应用边距。 developer.apple.com/documentation/uikit/uipopovercontroller/… 请通过编辑将所有解释添加到您的答案中以上是关于Popover 演示控制器 - 视图周围有间距,不适合全宽的主要内容,如果未能解决你的问题,请参考以下文章