UITextField的键盘是否必须占用整个屏幕?

kem*_*ica 14 uikeyboard swift tvos

看来键盘不必占用整个屏幕,请查看我帖子的问题栏中的UPDATE.谢谢.

描述

使用UITextField在屏幕上放置全屏键盘.

参考

我已经设置了一个UISplitViewController,我希望RootViewController(又名MasterViewController)有UITextField键盘显示.然后我想在右边搜索结果(在"ResultViewController"(UIViewController)中).

这个想法是当用户输入时,提出结果.

我尝试过的:

我首先添加了一个UITextField到我的RootViewController通道,storyboard但是当我通过激活键盘时占用了整个屏幕textField.becomeFirstResponder().

我想如果我使用了UIAlertController这个问题,但是键盘仍然会占据整个屏幕.

class RootViewController: UIViewController {

 override func viewDidLoad() {

    let alertController = UIAlertController(title: "Search", message: "Search for something!.", preferredStyle: UIAlertControllerStyle.Alert)

    alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
        textField.placeholder = "Search"
    })
    self.addChildViewController(alertController)
    alertController.view.frame = self.view.frame
    self.view.addSubview(alertController.view)
 }
}
Run Code Online (Sandbox Code Playgroud)

题:

使用UISplitViewController时我怎样才能让键盘只停留在其中RootViewController并且不占用整个屏幕?

更新:看来这已在新苹果电视上的Netflix应用程序中实现.键盘位于顶部,占据整个宽度.底部分为两部分.左侧是建议的单词结果,右侧是可能视频的视频封面的集合视图.一切都在用户输入时更新.

如果这被认为是Apple TV的糟糕设计,那么请随时指出原因.

截屏:

这就是我目前得到的.文本框打开一个占据整个屏幕的键盘.

在此输入图像描述

JAL*_*JAL 6

使用内置SDK方法无法仅在屏幕的一部分上显示键盘.

A UIKeyboard显示在应用程序窗口上,而不是任何子视图上.要完成所需的行为,请使用反射来获取UIKeyboard应用程序对象的引用UIWindow(通过迭代窗口的子视图),并更改其框架以匹配您的宽度RootViewController.

首先,您可以在此处查看私有UIKeyboard.h tvOS标头.我应该注意到Apple可能有代码来禁用键盘的大小调整(例如在willMoveToWindow:didMoveToWindow等).这是不确定的行为,如果它甚至可以工作,你的milage会有所不同.

您已删除了我对Netflix应用程序的回答的评论,但正如shirefriendship在回答中所说,Netflix可能使用了TVML模板.你可以走这条路,建立一个混合的tvOS/TVML应用程序.此外,你也可以手动建立自己键盘用UICollectionView在你的RootViewController.


kem*_*ica 4

介绍:

我终于能够实现这个而无需使用TVML templates. 最终的解决方案看起来像这样:

文本键盘和结果在一个视图上

总体思路是创建一个UICollectionViewController带有 a 的UICollectionViewCell. 然后以编程方式添加键盘并TabViewController通过您的AppDelegate.


如何使用结果实现此搜索视图:

第 1 步:创建故事板和控制器

打开故事板并创建一个UICollectionViewControllerSearchResultViewController附加您的TabViewController.

在其中创建一个UICollectionViewCell包含您想要的任何内容的labelsimagesUICollectionViewCell应该有一个名为“”的自定义类VideoSearchCell

你的 SearchViewController 里面应该没有其他东西。

第 2 步:通过编程方式SearchViewController添加TabViewController和实现键盘AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {

    }
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        if let tabController = window?.rootViewController as? UITabBarController {
            tabController.viewControllers?.append(configueSearchController())
        }

        return true
    }

    //... standard code in-between

    func configueSearchController() -> UIViewController {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let searchResultController = storyboard.instantiateViewControllerWithIdentifier(SearchResultViewController.storyboardIdentifier) as? SearchResultViewController else {
            fatalError("Unable to instatiate a SearchResultViewController from the storyboard.")
        }

        /*
         Create a UISearchController, passing the `searchResultsController` to
         use to display search results.
         */
        let searchController = UISearchController(searchResultsController: searchResultsController)
        searchController.searchResultsUpdater = searchResultsController
        searchController.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. Gastric Bypass)", comment: "")

        // Contain the `UISearchController` in a `UISearchContainerViewController`.
        let searchContainer = UISearchContainerViewController(searchController: searchController)
        searchContainer.title = NSLocalizedString("Search", comment: "")

        // Finally contain the `UISearchContainerViewController` in a `UINavigationController`.
        let searchNavigationController = UINavigationController(rootViewController: searchContainer)
        return searchNavigationController

    }

}
Run Code Online (Sandbox Code Playgroud)

添加 SearchResultViewController 的基本框架后,当您运行项目时,您应该能够在搜索视图顶部看到键盘。

第 3 步:处理文本输入并更新结果

您会注意到,在我的文章中filterString,我使用了一个名为 ScoreVideo 的类和一个 StringSearchService。这些只是我用来过滤视频列表的类(又名:self.vms.videos)。

所以最后,只需filterString创建一个新的过滤列表并重新加载您的集合视图。

import UIKit
import Foundation

class SearchResultViewController: UICollectionViewController, UISearchResultsUpdating {


    //private let cellComposer = DataItemCellComposer()
    private var vms: VideoManagerService!

    private var filteredVideos = [ScoreVideo]()
    static let storyboardIdentifier = "SearchResultViewController"

    var filterString = "" {
        didSet {
            // Return if the filter string hasn't changed.
            guard filterString != oldValue else { return }
                // Apply the filter or show all items if the filter string is empty.
                if self.filterString.isEmpty {
                    self.filteredVideos = StringSearchService.start(self.filterString, videos: self.vms.videos)
                }
                else {
                    self.filteredVideos = StringSearchService.start(self.filterString, videos: self.vms.videos)
                }
           self.collectionView?.reloadData()
        }
    }

    override func viewDidLoad() {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        self.vms = appDelegate.getVideoManagerService()

    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("count..\(filteredVideos.count)")
        return filteredVideos.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // Dequeue a cell from the collection view.
        return collectionView.dequeueReusableCellWithReuseIdentifier(VideoSearchCell.reuseIdentifier, forIndexPath: indexPath)
    }

    // MARK: UICollectionViewDelegate

    override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        guard let cell = cell as? VideoSearchCell else { fatalError("Expected to display a `VideoSearchCell`.") }
        let item = filteredVideos[indexPath.row]
        cell.configureCell(item.video)

    }

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        dismissViewControllerAnimated(true, completion: nil)
    }

    // MARK: UISearchResultsUpdating

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        print("updating... \(searchController.searchBar.text)")
        filterString = searchController.searchBar.text!.lowercaseString ?? ""
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有不清楚的地方,请随时提出一些问题。我很可能忘记了什么。谢谢。

答案灵感来自苹果示例代码