[英]ios13 UIPopoverViewController showing UITableViewController - Safe Area problems / Missing parts of table
在與這篇文章相同的主題中:
iOS 13 - UIPopoverPresentationController sourceview 內容在箭頭中可見
我有一個從 Storyboard 實例化的 UITableViewController。 我在 UIPopoverViewController 中展示它。
根據方向,我要么缺少一側,要么缺少頂部,_UITableViewHeaderFooterViewBackground 的內容會“通過”箭頭滾動。
怎么了:
這些是它在頂部的時候,但如果它出現在側面,那么整個側面
編輯:我正在使用安全區域指南:
正如我之前所說,我從 object 庫中拖出一個全新的 UITVC,然后將原型單元更改為我的應用程序的要求。
我對任何安全區域的切換或設置進行了零更改。
我不會更改代碼中的任何安全區域插圖或調整。
所以,謝謝@Alex,但這不是問題,或者如果是,我不知道該怎么辦。
編輯2:
今天下午,我確保我的課程中有零代碼,可以執行任何格式、顏色、插入或與任何形式的更改默認表格單元格有關的任何事情。
然后我在 IB 中完全刪除了我的 UITVC 並創建了一個新的,創建沒有樣式的新原型單元,除了將 UIImageView 放入單元中。
我確保 UITVC、tableviewcell 和內容視圖都在 IB 中勾選了“安全區域”和“安全邊距”。
重建應用程序並運行。
完全相同的。
我仍然有內容和背景都在箭頭上,如果我然后在桌子周圍添加一個邊框,則呈現側被“切斷”。
編輯 3:
我知道問題是什么!!!
這是 _UITableViewHeaderFooterViewBackground 不能很好地與 AutoLayout 一起使用!
我不知道如何解決它,但這就是問題所在。
編輯 4 / 更新:
今天我構建了以下應用程序,並且錯誤仍然存在 - 即使沒有節標題!
在 IB 中,創建一個帶有兩個按鈕的 UIVC,一個左上角,另一個右上角。
從 Asset Catalog 中拖動一個 UIVC 並將其放在原始 VC 的一側,然后從 Asset Library 中拖動一個 UIVC 並將其放在另一側。
將 UITableView 從資源庫拖到“新” UIViewController 上,只需將其頂部和底部設置為引導安全區域布局,將左側和右側設置為安全邊距,常量為 0(您可以保留默認值 20 - 沒有區別)。
現在,將這些按鈕連接到原始 UIVC,以便按下每個按鈕實例化一個或另外兩個控制器,並在彈出窗口中顯示它們。
請注意,沒有任何格式,沒有樣式,沒有更改任何 UIViewControllers 上的任何默認 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 中的布局 - 請注意,我已經以不同方式更改了所有視圖的背景顏色,因此您可以看到哪些視圖導致了問題。
這里是來自每一方 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配置:
您似乎沒有使用新的安全區域布局指南。 舊方法已棄用。 如果您使用 storyboard,請在“文件”選項卡中激活此設置:
在代碼中,您可以使用如下內容:
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/
好的,所以我用了蘋果的支持票——在這里一年后沒有答案。
由於 UITableViewController 和它的 UITableView 的引擎蓋下自動布局的東西,舊的方式(在彈出框上設置邊框)將永遠不會再次起作用。
唯一的解決方案是完全刪除 UITableViewController,並將其替換為 UIViewController,其中包含一個視圖,然后在該視圖內放置一個 UITableView。
幸運的是,代碼更改很少(只需將 inheritance 從 UITableViewController 更改為 UIViewController,然后添加 UITableViewDelegate)
然后確保包含表的 UIViewController 將自身設置為 UITableView 的委托和源。
修復中最長的部分是在 Interface Builder 中重新創建布局,但這次確保 UITableView 沒有得到它的頂部、前導、底部、尾隨到 SUPERVIEW,而是到安全區域。
然后你需要做的就是把它放進去,它就可以工作了:
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.layer.borderWidth = 5
self.tableView.layer.borderColor = UIColor.white.cgColor
self.tableView.layer.cornerRadius = 16
}
我有同樣的問題,並沒有找到任何方法來解決這個問題。 從我的角度來看,這是一個錯誤。 增加主視圖以包含箭頭是沒有意義的。 背景顏色正確填充視圖,而邊框無法這樣做。 如果您創建一個新的簡單的 hello world popover 應用程序,其中包含安全區域等,您會遇到同樣的問題。
我找到了答案。 我必須實現我自己的 UIPopoverBackgroundView。 示例: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch09p476popovers/ch22p751popovers/MyPopoverBackgroundView.swift它適用於我。 我的彈出窗口
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.