【问题标题】:tvOS SearchController, avoid collapsing keyboardtvOS SearchController,避免折叠键盘
【发布时间】:2020-06-15 07:38:50
【问题描述】:

我在 tvOS 中创建了一个 SearchController,它在一个带有水平滚动的 CollectionView 中显示结果。

最终结果看起来很糟糕,因为当焦点从键盘移到结果时,键盘会自动关闭。

因为没有键盘的collection view的高度更大,它上面的元素会在collection view中自动重新对齐。这让用户体验非常混乱。

您可以在以下 GIF 中看到问题。将焦点从键盘向下移动到结果,最终甚至聚焦非预期元素。 (“七”而不是“五”)

有什么办法可以避免键盘崩溃?我注意到当结果视图包含不可滚动视图时,键盘不会折叠,但我需要使结果可滚动。

您可以在此处找到重现该问题的代码。

import UIKit

private var items = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"]

final class MySearchViewController: UIViewController, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpView()
    }

    // MARK: - Private

    private lazy var searchContainerViewController: UISearchContainerViewController = {
        return UISearchContainerViewController(searchController: searchController)
    }()

    private lazy var searchController: UISearchController = {
        let searchController = UISearchController(searchResultsController: searchResultsController)
        return searchController
    }()

    private lazy var searchResultsController: UIViewController = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        let searchResultsController = UICollectionViewController(collectionViewLayout: layout)
        searchResultsController.collectionView.dataSource = self
        MyCell.register(in: searchResultsController.collectionView)
        return searchResultsController
    }()

    private func setUpView() {
        embed(viewController: searchContainerViewController, inContainerView: view)
    }

    // MARK: - UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCell.reuseIdentifier, for: indexPath) as! MyCell
        cell.titleLabel.text = items[indexPath.row]
        return cell
    }
}

class MyCell: UICollectionViewCell {

    static var reuseIdentifier: String { return String(describing: self) + "ReuseIdentifier" }

    var titleLabel: UILabel!

    public static func register(in collectionView: UICollectionView) {
        collectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel = UILabel(frame: bounds)
        backgroundColor = .blue
        contentView.addSubview(titleLabel)
    }

    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        backgroundColor = isFocused ? .red : .blue
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

避免聚焦错误元素的解决方法是为 CollectionView 分配固定高度。但是这样做,仍然隐藏了键盘,在屏幕上留下了很多多余的空白空间。

因为在这种情况下隐藏键盘是没有用的。我想让它始终可见。

这是对集合视图使用固定高度的结果。

【问题讨论】:

    标签: ios swift tvos apple-tv


    【解决方案1】:

    直到 tvOS 14 才真正实现,当时 Apple 发布了新属性 searchControllerObservedScrollView: https://developer.apple.com/documentation/uikit/uisearchcontroller/3584820-searchcontrollerobservedscrollvi

    通过在此属性上设置您的 collectionView,SearchController 将自动将搜索栏 + 键盘位置调整为您的 CollectionView 偏移量。

    你可以这样调整你的代码:

        private lazy var searchController: UISearchController = {
            let searchController = UISearchController(searchResultsController: searchResultsController)
            if #available(tvOS 14.0, *) {
                searchController.searchControllerObservedScrollView = (searchResultsController as? UICollectionViewController)?.collectionView
            }
            return searchController
        }()
    

    要摆脱可为空的强制转换,只需将searchResultsController 返回类型更改为UICollectionViewController。它不应影响您的实施。

    【讨论】:

    • 您知道如何在 SwiftUI 上解决此问题吗?我认为 TVOS 15 可以使用可搜索的修饰符...提前致谢
    • Apple 的文档说它是在 tvOS 13 中引入的。你确定它需要 tvOS 14 吗?
    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 2018-01-10
    • 2016-12-23
    • 2021-05-31
    • 2011-05-20
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多