FloatingPanel 未出现 - Swift Xcode

FloatingPanel not appearing - Swift Xcode

我有一个包含 table 视图的视图控制器。在这个视图控制器中,我还设置了一个浮动面板。但是,由于某种原因,浮动面板没有出现。我知道 ContentViewController 正在工作,所以我没有包含该代码。错误在这段代码的某处,我找不到它。这就像 FloatingPanel 只是没有被添加到视图中。

import Foundation
import UIKit
import FloatingPanel

class Section {
    let title: String
    let options: [String]
    var isOpened: Bool = false
    
    init(title: String,
         options: [String],
         isOpened: Bool
    ) {
        self.title = title
        self.options = options
        self.isOpened = isOpened
    }
    
}

class PicsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, FloatingPanelControllerDelegate {
    
    @IBOutlet weak var addButton: UIButton!
    
    
    private let picsTableView: UITableView = {
        let picsTableView = UITableView()
        picsTableView.register(UITableViewCell.self,
                               forCellReuseIdentifier: "cell")
        return picsTableView
    }()
    
    private var sections = [Section]()
    let backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    
    
    /*------------START FLOATING PANEL-------------*/
    var fpc: FloatingPanelController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("PicsViewController LAUNCHED!!")
        fpc = FloatingPanelController()
        fpc.delegate = self
//        guard let contentVC = storyboard?.instantiateViewController(identifier: "fpc_content") as? ContentViewController else {
//            print("Failed to instantiate storyboard")
//            return
//        }
        let contentVC = ContentViewController()
        
        fpc.set(contentViewController: contentVC)
//        fpc.track(scrollView: contentVC.collectionView)
        fpc.addPanel(toParent: self)
        
    /*------------END FLOATING PANEL-------------*/
        
        
        // Set up models
        picsTableView.delegate = self
        picsTableView.dataSource = self
        picsTableView.frame = view.bounds
        picsTableView.backgroundColor = backgroundColor
        view.addSubview(picsTableView)
        sections = [
            Section(title: "Section 1", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 2", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 3", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 4", options: ["Test 1", "Test 2", "Test 3"], isOpened: false)
        ]
        // get rid of extra table view cells
        picsTableView.tableFooterView = UIView()
        
        // customize button
        addButton.layer.cornerRadius = 0.5 * addButton.bounds.size.width
        addButton.clipsToBounds = true
        addButton.backgroundColor = .red
        addButton.tintColor = .white
        addButton.setImage(#imageLiteral(resourceName: "plus-5-512"), for: .normal)
        view.bringSubviewToFront(addButton)
        
    }
    
    
    @IBAction func addButtonTapped(_ sender: Any) {
        print("Add button tapped")
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let section = sections[section]
        
        if section.isOpened {
            return section.options.count + 1
        } else {
            return 1
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = picsTableView.dequeueReusableCell(
            withIdentifier: "cell",
            for: indexPath
        )
        cell.backgroundColor = .clear
        cell.textLabel?.textColor = .black
        
        if indexPath.row == 0 {
            cell.textLabel?.text = sections[indexPath.section].title
        } else {
            cell.textLabel?.text = sections[indexPath.section].options[indexPath.row - 1]
        }
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        picsTableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row == 0 {
            sections[indexPath.section].isOpened = !sections[indexPath.section].isOpened
            picsTableView.reloadSections([indexPath.section], with: .none)
        } else {
            print("Tapped sub cell \(indexPath.row)")
        }
    }
    
    
}

通常,将任何视图控制器作为子视图添加到任何视图控制器的 3 或 4 个语句是

        let viewController = SomeViewController()
        self.addChild(viewController)
        self.view.addSubview(viewController.view)
        viewController.didMove(toParent: self)

因为你没有在你调用 fpc.addPanel(toParent: self) 的地方提供 addPanel 的实现,我假设它做了类似于我上面写的东西,如果我要写它,我可能会写像

    func addPanel(toParent: UIViewController) {
        toParent.addChild(self)
        toParent.view.addSubview(self.view)
        //add constraints or set frames for your subview
        self.didMove(toParent: toParent)
    }

最后,您要向视图控制器的视图添加多个视图,即浮动面板、tableView 和按钮。因为你在添加所有其他视图之前添加面板,它可能隐藏在其他子视图下,如 tableView,你显然可以使用视图调试器检查这个假设,修复它的最简单方法是使用 bringSubviewToFront,但问题是您没有持有 FloatingPanelController 视图的引用,因此您可以尝试添加 self.view.bringSubviewToFront(fpc.view) 作为 ViewDidLoad

的最后一条语句
override func viewDidLoad() {
        super.viewDidLoad()
        // all other codes of yours
        self.view.bringSubviewToFront(fpc.view)
}

并且出于某种原因,如果那不起作用调用 fpc.addPanel(toParent: self) 作为 ViewDidLoad 的最后一条语句,而不是在两者之间添加并添加 bringSubviewToFront 作为 addPanel(toParent 方法的最后一条语句

    override func viewDidLoad() {
        super.viewDidLoad()
        print("PicsViewController LAUNCHED!!")
        fpc = FloatingPanelController()
        fpc.delegate = self
        let contentVC = ContentViewController()

        fpc.set(contentViewController: contentVC)
        picsTableView.delegate = self
        picsTableView.dataSource = self
        picsTableView.frame = view.bounds
        picsTableView.backgroundColor = backgroundColor
        view.addSubview(picsTableView)
        sections = [
            Section(title: "Section 1", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 2", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 3", options: ["Test 1", "Test 2", "Test 3"], isOpened: false),
            Section(title: "Section 4", options: ["Test 1", "Test 2", "Test 3"], isOpened: false)
        ]
        // get rid of extra table view cells
        picsTableView.tableFooterView = UIView()

        // customize button
        addButton.layer.cornerRadius = 0.5 * addButton.bounds.size.width
        addButton.clipsToBounds = true
        addButton.backgroundColor = .red
        addButton.tintColor = .white
        addButton.setImage(#imageLiteral(resourceName: "plus-5-512"), for: .normal)
        view.bringSubviewToFront(addButton)

        fpc.addPanel(toParent: self) //added as last statement
    }

func addPanel(toParent: UIViewController) {
        toParent.addChild(self)
        toParent.view.addSubview(self.view)
        self.didMove(toParent: toParent)
        toParent.view.bringSubviewToFront(self.view)
}