【问题标题】:iOS: Problem of display the popover border in the iOS13iOS:iOS13显示popover边框的问题
【发布时间】:2019-09-10 08:24:42
【问题描述】:

我显示的弹出框现在显示不正确。箭头一侧缺少一条线。我们可以检查箭头末端是否有一小块黑色。我觉得里面的景色太长了。

显示弹出框的代码:

   _popoverController = UIPopoverController(contentViewController: navController)
   _popoverController?.delegate = self

   let rect = slotCollectionView.cellForItem(at: indexPath)!.frame
   self._popoverController?.backgroundColor = UIColor.init(rgb: Int(quaternaryColorHexa))
   self._popoverController?.present (from: rect, in: self.slotCollectionView, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true) 

初始化 Popover 的代码:

override func viewDidLoad()
{
    super.viewDidLoad()

    self.preferredContentSize = contentSize()
    self.navigationController!.preferredContentSize = self.preferredContentSize;

    peopleTableView.isScrollEnabled = true
    peopleTableView.bounces = true
    peopleTableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    peopleTableView.tableFooterView?.isHidden = true
    peopleTableView.backgroundColor = UIColor.init(rgb: Int(quinquenaryColorHexa))

    self.view.backgroundColor = UIColor.init(rgb: Int(quinquenaryColorHexa))
    self.view.layer.cornerRadius = 13.0
    self.view.layer.borderWidth = 1.5
    self.view.layer.borderColor = UIColor.init(rgb: Int(quaternaryColorHexa)).cgColor 

iOS12显示:

iOS13显示:

【问题讨论】:

  • 可能是因为它是deprecated since iOS 9.0 并且Apple 不支持与此UI 组件相关的API。使用另一种方法。
  • 更现代的 modalPresentationStyle = UIModalPresentationPopover 也存在同样的问题;接近

标签: ios swift uipopovercontroller


【解决方案1】:

正如this answer 中提到的,UIPoovers 在其内容视图中包含箭头是 iOS 13 的一项新功能。您应该使用安全区域来正确应对这种变化。

【讨论】:

  • @iOSProgrammingIsFun 使用自动布局时,将您的顶视图位置链接到顶部安全区域布局指南而不是超级视图。这应该足够了。如果是 UITableView 内容/滚动插图:您是否尝试将 tableViews 插图设置为视图安全区域?也许tableView.safeAreaInsets 忽略了弹出框安全区域。
  • 这里有一些例子:stackoverflow.com/questions/58149863/…这是我的问题。
【解决方案2】:

我认为是iOS13版本的一个iOS bug,建议你自己用那个git项目做popover:
DDPopoverBackgroundView

并使用它来显示弹出框:

       // Popover
       _popoverController = UIPopoverController(contentViewController: navController)
       _popoverController?.delegate = self

       let rect = slotCollectionView.cellForItem(at: indexPath)!.frame

       self._popoverController!.contentSize = CGSize(width: 350, height: 600)

       self._popoverController!.backgroundViewClass = DDPopoverBackgroundView.self
       self._popoverController!.backgroundColor = UIColor.init(rgb: Int(quaternaryColorHexa)) //arrow color

       OperationQueue.main.addOperation({
           self._popoverController?.present(from: rect, in: self.slotCollectionView, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)
       })

享受吧! ;-)

【讨论】:

    【解决方案3】:

    您无法使用安全区域来对此更改做出反应。我的感觉是这是 iOS 13 中的一个错误,我会解释为什么......

    当您遇到使用 UIPopoverArrowDirectionAny 的情况时,您会发现问题仅在箭头位于弹出框的顶部或左侧时存在,而不是在箭头出现在弹出框的右侧或底部时存在。如果您在代码中进行调整以进行补偿,如果您使用 UIPopoverArrowDirectionUp 或 UIPopoverArrowDirectionLeft,它将起作用,但在使用 UIPopoverArrowDirectionAny 时使用该调整将无法正确显示,并且弹出窗口出现在目标矩形的上方或右侧。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。我的解决方案如下:

      class MyPopoverViewController: UIViewController {
          override func viewWillAppear(_ animated: Bool) {
              let borderColor = UIColor.black
              let borderWidth = CGFloat(1.5)
              let cornerRadius = CGFloat(13)
              let extensionWidth = CGFloat(20)
      
              if #available(iOS 13, *), let superview = self.view.superview {
                  let guide = self.view.safeAreaLayoutGuide
                  let borderView = UIView()
                  borderView.isUserInteractionEnabled = false
                  borderView.backgroundColor = UIColor.clear
                  borderView.layer.borderWidth = borderWidth + extensionWidth
                  borderView.layer.cornerRadius = cornerRadius + extensionWidth
                  borderView.layer.borderColor = borderColor.cgColor
                  borderView.translatesAutoresizingMaskIntoConstraints = false
                  superview.addSubview(borderView)
                  NSLayoutConstraint.activate([
                      borderView.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: -extensionWidth),
                      borderView.topAnchor.constraint(equalTo: guide.topAnchor, constant: -extensionWidth),
                      borderView.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: extensionWidth),
                      borderView.rightAnchor.constraint(equalTo: guide.rightAnchor, constant: extensionWidth)
                  ])
              } else {
                  self.view.layer.borderWidth = borderWidth
                  self.view.layer.cornerRadius = cornerRadius
                  self.view.layer.borderColor = borderColor.cgColor
                  self.view.clipsToBounds = true
                  self.popoverPresentationController?.backgroundColor = borderColor
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 2010-11-23
        • 2022-07-26
        • 2015-10-17
        • 1970-01-01
        • 2019-01-21
        • 2018-09-28
        相关资源
        最近更新 更多