【发布时间】:2020-08-31 09:02:21
【问题描述】:
我正在尝试在我的 TVOS 应用中实现下拉菜单。显然,正确的做法是使用 UITextFieldView 和 UIPickerView。
结帐答案here.
问题是 UIPickerView 是not available in TVOS。
这种方法有没有可靠的替代方法?
【问题讨论】:
标签: ios objective-c swift xcode tvos
我正在尝试在我的 TVOS 应用中实现下拉菜单。显然,正确的做法是使用 UITextFieldView 和 UIPickerView。
结帐答案here.
问题是 UIPickerView 是not available in TVOS。
这种方法有没有可靠的替代方法?
【问题讨论】:
标签: ios objective-c swift xcode 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, width: frames.width, height: CGFloat(self.dataSource.count * 50))
}, completion: nil)
}
@objc func removeTransparentView() {
let frames = selectedButton.frame
UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
self.transparentView.alpha = 0
self.tableView.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height, width: frames.width, 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()
}
}
【讨论】: