如何将导航栏和标签栏设置为通过 UIPopoverViewController 的视图?

How to set Navigation bar and Tab bar as pass through views of a UIPopoverViewController?

我将 UIViewController 自定义为 PopoverViewController,当我展示它时,我无法触摸导航栏和标签栏

我搜索了一会儿,得到了: 在第一个 ViewController 代码的准备中(对于 segue:UIStoryboardSegue,发件人:Any?):

if segue.identifier == "ShowChoice"{
      let poper = segue.destination
      poper.modalPresentationStyle = .popover
      poper.popoverPresentationController?.delegate = self
      let point = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY + 5)
      poper.popoverPresentationController?.sourceView = self.view
      poper.popoverPresentationController?.sourceRect = CGRect(origin: point, size: CGSize.zero)
      poper.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

//here I set [self.view] to passthroughViews and it work i can interact with all thing in self.view but I can't interact with Navigation Button or Tab bar item!

            poper.popoverPresentationController?.passthroughViews = [self.view]
        }
    }

我只能将 UIView 设置为 passthroughViews,但我想设置 Navigation Bar 或 Tab Bar

谢谢!

我终于自己找到了,只需将 [self.tabBarController?.tabBar,self.navigationController?.navigationBar] 转换为 [UIView]

poper.popoverPresentationController?.passthroughViews = [self.tabBarController?.tabBar,self.navigationController?.navigationBar] as! [UIView]

有趣的是,popoverPresentationController.passthroughViews 中的项目顺序很重要!

例如,如果您想添加所有视图和导航栏,则必须将导航栏放在列表的开头,否则触摸只会被提供给它下面的任何项目

这是我的代码:

 popoverPresentationController.passthroughViews = [self.view]
 if let navBar = self.navigationController?.navigationBar {
     popoverPresentationController.passthroughViews?.insert(navBar, at: 0)
 }

此外,如果您这样做是为了能够使用后退按钮,请记得关闭 popoverView