如何在不使用UIPickerView的情况下在tvOS中实现下拉列表?

问题描述

我正在尝试在TVOS应用程序中实现下拉菜单。显然,正确的方法是将UITextFieldView和UIPickerView一起使用。
结帐答案here
问题是UIPickerView是not available in TVOS
除了这种方法,还有其他可靠的替代方法吗?

解决方法

通过遵循此tutorial,我发现了另一种方式。但是,这不是一个完美的解决方案,因为它在TVOS中的行为有所不同。 这是您应该使用的代码:

import UIKit

class CellClass: UITableViewCell {
    
}

class TestDropDownViewController: UIViewController {

    @IBOutlet weak var btnSelectFruit: UIButton!
    @IBOutlet weak var btnSelectGender: UIButton!
    
    let transparentView = UIView()
    let tableView = UITableView()
    
    var selectedButton = UIButton()
    
    var dataSource = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(CellClass.self,forCellReuseIdentifier: "Cell")
    }
    
    func addTransparentView(frames: CGRect) {
        let window = UIApplication.shared.keyWindow
        transparentView.frame = window?.frame ?? self.view.frame
        self.view.addSubview(transparentView)
        
        tableView.frame = CGRect(x: frames.origin.x,y: frames.origin.y + frames.height,width: frames.width,height: 0)
        self.view.addSubview(tableView)
        tableView.layer.cornerRadius = 5
        
        transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.9)
        tableView.reloadData()
        let tapgesture = UITapGestureRecognizer(target: self,action: #selector(removeTransparentView))
        transparentView.addGestureRecognizer(tapgesture)
        transparentView.alpha = 0
        UIView.animate(withDuration: 0.4,delay: 0.0,usingSpringWithDamping: 1.0,initialSpringVelocity: 1.0,options: .curveEaseInOut,animations: {
            self.transparentView.alpha = 0.5
            self.tableView.frame = CGRect(x: frames.origin.x,y: frames.origin.y + frames.height + 5,height: CGFloat(self.dataSource.count * 50))
        },completion: nil)
    }
    
    @objc func removeTransparentView() {
        let frames = selectedButton.frame
        UIView.animate(withDuration: 0.4,animations: {
            self.transparentView.alpha = 0
            self.tableView.frame = CGRect(x: frames.origin.x,height: 0)
        },completion: nil)
    }

    @IBAction func onClickSelectFruit(_ sender: Any) {
        dataSource = ["Apple","Mango","Orange"]
        selectedButton = btnSelectFruit
        addTransparentView(frames: btnSelectFruit.frame)
    }
    
    @IBAction func onClickSelectGender(_ sender: Any) {
        dataSource = ["Male","Female"]
        selectedButton = btnSelectGender
        addTransparentView(frames: btnSelectGender.frame)
    }
}

extension TestDropDownViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
        cell.textLabel?.text = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        selectedButton.setTitle(dataSource[indexPath.row],for: .normal)
        removeTransparentView()
    }
}