swift - ios13 UIPopoverViewController 显示 UITableViewController - 安全区域问题/表格缺失部分

标签 swift uitableview uipopovercontroller ios13 safearealayoutguide

与本文主题相同:

iOS 13 - UIPopoverPresentationController sourceview content visible in the arrow

我有一个从 Storyboard 中实例化的 UITableViewController。我在 UIPopoverViewController 中呈现它。

根据方向,我要么缺少一侧,要么缺少顶部,并且 _UITableViewHeaderFooterViewBackground 的内容“通过”箭头滚动。

出了什么问题:

这些是当它在顶部时,但如果它出现在侧面则整个侧面

enter image description here enter image description here

enter image description here

编辑:我正在使用安全区域指南:

enter image description here

正如我之前所说,我从对象库中拖出一个全新的 UITVC,然后将原型(prototype)单元更改为我的应用程序的要求。

我对任何安全区域切换或设置进行了零更改。

我不会更改任何安全区域插入或代码调整。

谢谢@Alex,但这不是问题所在,如果是,我不知道该怎么做。

编辑 2:

今天下午,我确保我的类中有零代码可以执行任何格式设置、颜色、插入或与任何形式的更改默认表格单元格有关的任何事情。

然后我在 IB 中完全删除了我的 UITVC 并创建了一个新的,创建了一个没有样式的新原型(prototype)单元格,除了在单元格中放置了一个 UIImageView。

我确保 UITVC、tableviewcell 和内容 View 都在 IB 中勾选了“安全区域”和“安全边距”。

重建应用程序并运行。

完全一样。

我仍然让内容和背景越过箭头,如果我随后在表格周围添加边框,则呈现面将被“切断”。

编辑 3:

我知道问题出在哪里!!!

这是 _UITableViewHeaderFooterViewBackground 不能很好地与 AutoLayout 一起玩!!

我不确定如何修复它,但这就是问题所在。

enter image description here

编辑 4/更新:

今天我构建了以下应用程序,但错误仍然存​​在 - 即使没有 Section Headers!

  1. 在 IB 中,创建一个带有两个按钮的 UIVC,一个在左上,另一个在右上。

  2. 从 Asset Catalog 中拖出一个 UITVC 并将其放在原始 VC 的一侧,然后从 Asset Library 中拖出一个 UIVC 并将其放在另一侧。

  3. 将资源库中的 UITableView 拖到“新的”UIViewController 上,并将其顶部和底部设置为指南安全区域布局,将左侧和右侧设置为安全边距,并设置为 0 常量(您可以将默认 20 - 没有区别)。

  4. 现在,将这些按钮连接到原始 UIVC,以便按下每个按钮时实例化一个或另外两个 Controller ,并将它们显示在弹出窗口中。

注意,没有格式,没有样式,没有更改任何 UIViewController 上的任何默认 IB 设置。

这是“中心”VC 代码:

import UIKit



class ViewController: UIViewController, UIPopoverPresentationControllerDelegate
{
    @IBOutlet weak var tbutton: UIButton!
    @IBOutlet weak var button: UIButton!



    @IBAction func buttonTouched(_ sender: UIButton)
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let libraryVC = storyboard.instantiateViewController(withIdentifier: "library")

        libraryVC.modalPresentationStyle = .popover

        if let popover = libraryVC.popoverPresentationController
        {
            popover.sourceView = self.button
            popover.sourceRect = self.button.bounds

            popover.delegate = self
        }

        libraryVC.preferredContentSize = CGSize(width: 400, height: 2048)

        libraryVC.view.layer.borderColor  = UIColor.white.cgColor
        libraryVC.view.layer.borderWidth  = 5.0
        libraryVC.view.layer.cornerRadius = 16.0

        self.present(libraryVC, animated: true)

    }



    @IBAction func tbuttonTouched(_ sender: UIButton)
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let libraryVC = storyboard.instantiateViewController(withIdentifier: "tlibrary")

        libraryVC.modalPresentationStyle = .popover

        if let popover = libraryVC.popoverPresentationController
        {
            popover.sourceView = self.tbutton
            popover.sourceRect = self.tbutton.bounds

            popover.delegate = self
        }

        libraryVC.preferredContentSize = CGSize(width: 400, height: 2048)

        libraryVC.view.layer.borderColor  = UIColor.white.cgColor
        libraryVC.view.layer.borderWidth  = 5.0
        libraryVC.view.layer.cornerRadius = 16.0

        self.present(libraryVC, animated: true)
    }



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

这是 IB 中的布局 - 请注意,我已经以不同方式更改了所有 View 的背景颜色,以便您可以看到哪些 View 导致了问题。

enter image description here

这里是来自各个 VC 的代码:


import UIKit



class LibraryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    @IBOutlet weak var table: UITableView!



    override func viewDidLoad()
    {

    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        12
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as? MyTableViewCell else
        {
            fatalError("expected to dequeue MenuItemTableCell - check storyboard")
        }

        return cell
    }
}

和另一个:

import UIKit



class LibraryTableViewController: UITableViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
    }



    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        // #warning Incomplete implementation, return the number of rows
        return 10
    }



    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as? MyTableViewCell else
        {
            fatalError("expected to dequeue MenuItemTableCell - check storyboard")
        }

        return cell
    }
}

这是 IB 配置:

enter image description here

enter image description here 结果如下:

enter image description here enter image description here

enter image description here

enter image description here

最佳答案

您似乎没有使用新的安全区域布局指南。旧方法已弃用。如果您使用 Storyboard,请在"file"选项卡中激活此设置:

enter image description here

在代码中你可以使用这样的东西:

let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
 greenView.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),
 guide.bottomAnchor.constraintEqualToSystemSpacingBelow(greenView.bottomAnchor, multiplier: 1.0)
])

有关更多信息,请阅读:https://useyourloaf.com/blog/safe-area-layout-guide/

关于swift - ios13 UIPopoverViewController 显示 UITableViewController - 安全区域问题/表格缺失部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58149863/

相关文章:

ios - UIPopOverControl 和 UIImageView

swift - NavigationView 的内容未显示 SwiftUI

ios - 从 Firebafirebase 崩溃报告 OLD 迁移到 Firebase/Crashlytics

ios - iOS5 中我的 UITableView 出现 dequeueReusableCellWithIdentifier 错误

ios - 搜索结果在状态栏下方明显滚动

ios - UIPopoverController presentPopoverFromRect 显示不正确

ios - 防止一次显示多个 UIPopover

Swift:音频在 0.1 秒内开始和停止

ios - UIProgressView 仅更新值而不是 UI

ios - 如何在 iOS 6 中显示表格部分标题?