更改 UISearchController 视图的样式?

Change the style of the UISearchController views?

问题:

如何更改 tvOSUISearchController 的 color/theme/style?

(看来我需要设置 UIStatusBarStyle some way, since the tvOS 中不存在替代 UISearchController

描述:

在我的 AppDelegate 中调用的方法以编程方式创建一个 searchNavigationController,顶部是 UISearchController,底部是我的自定义 UITableViewController,名为“searchResultsController” (SearchViewController)

func configueSearchController() -> UIViewController {

    let storyboard = UIStoryboard(name: "Search", bundle: nil)
    guard let searchResultsController = storyboard.instantiateViewController(withIdentifier: SearchViewController.storyboardIdentifier) as? SearchViewController 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

}

上述方法的直观表示图像:

我尝试过的:

我尝试通过不同的方法改变样式,none 给出了预期的结果。

这对搜索栏没有影响:

searchController.searchBar.searchBarStyle = .minimal //or .prominent or .default

这只会使搜索栏(实际输入区域黑色..黑色文本..):

searchController.searchBar.backgroundColor = .black

这只会使 UISearchController 的整个背景视图变黑。一切都是黑色的,因此看不到键盘。

searchController.view.backgroundColor = .black

理想的解决方案是将主题从标准 .default 更改为 .dark。因为不仅背景颜色必须改变,边框和文本颜色也必须改变。

已尝试执行 :

//UISearchController
override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

但是,tvOS 似乎不存在此覆盖。有什么原因吗?

有解决此问题的方法,但我真的不明白为什么他们不保留与 iOS 相同的系统。无论如何,将 SearchBar 样式更改为黑色的主要操作是将 keyboardAppearance 设置为深色,将 searchController 的视图背景颜色设置为深色,并设置 searchBar 的背景颜色变白。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    guard let win = window else {
        return true
    }



    if let tabController = win.rootViewController as? UITabBarController {
        tabController.viewControllers?.append(configueSearchController())
    }

    win.backgroundColor = UIColor.white
    win.makeKeyAndVisible()

    return true
}   
func configueSearchController() -> UIViewController {

    let storyboard = UIStoryboard(name: "Search", bundle: nil)
    guard let searchResultsController = storyboard.instantiateViewController(withIdentifier: SearchViewController.storyboardIdentifier) as? SearchViewController 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: "")
    searchController.view.backgroundColor = UIColor.black
    searchController.searchBar.keyboardAppearance = UIKeyboardAppearance.dark
    searchController.searchBar.tintColor = UIColor.black
    searchController.searchBar.backgroundColor = UIColor.white
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.obscuresBackgroundDuringPresentation = true
    searchController.searchBar.searchBarStyle = .minimal
    searchController.searchBar.sizeToFit()

    //searchResultsController.tableView.tableHeaderView = searchController.searchBar

    // Contain the `UISearchController` in a `UISearchContainerViewController`.
    let searchContainer: UISearchContainerViewController = UISearchContainerViewController(searchController: searchController)

    // Finally contain the `UISearchContainerViewController` in a `UINavigationController`.
    let searchNavigationController = UINavigationController(rootViewController: searchContainer)
    searchNavigationController.navigationBar.isTranslucent = true
    searchNavigationController.navigationBar.tintColor = .black
    searchNavigationController.tabBarItem.title = "Search"
    return searchNavigationController

}

我不是 tvOS 方面的专家,但这是我的发现。 我从 Apple 的 UIKit Catalog: Creating and Customizing UIKit Controls 示例代码开始。我注意到的第一件事是,您在上面提供的代码将 searchResultsController 实例化为 SearchViewController,而 Apple 的示例代码将 searchResultsController 实例化为 SearchResultsViewController。我假设你打算这样做。

在试用示例代码后,我只能对样式进行非常有限的更改。这是我最接近您想要实现的目标的屏幕截图。

这里是影响该更改的代码。

func packagedSearchController() -> UIViewController {
    // Load a `SearchResultsViewController` from its storyboard.
    let storyboard = UIStoryboard(name: "ViewControllerSamples", bundle: nil)
    guard let searchResultsController = storyboard.instantiateViewController(withIdentifier: SearchResultsViewController.storyboardIdentifier) as? SearchResultsViewController else {
        fatalError("Unable to instatiate a SearchResultsViewController 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. iceland)", comment: "")

    //** This is the part you are interested in **//
    searchResultsController.view.backgroundColor = .black
    searchController.view.backgroundColor = UIColor(red: 0.25, green: 0.25, blue: 0.25, alpha: 1.0)
    searchController.searchBar.backgroundColor = .white

    // 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
}

不幸的是,我无法在黑暗中不丢失键盘的情况下将 SearchController 的视图设置为 .black。我寻找并寻找解决键盘消失问题的方法,认为必须有一种方法来设置键盘样式,或者至少修改选择状态,但都无济于事。我的失望在 App Programming Guide for tvOS: Designing the Keyboard Experience 的这篇声明中达到了顶峰;

An inline keyboard displays all of the possible inputs on a single line. Use UISearchController to create a keyboard that can be fully integrated with third-party content. However, there are very few customization options when you use UISearchController. You can not access the text field itself, customize the traits, or add input accessories.

我怀疑如果您能获得对该键盘的引用,您可能会影响某些更改。话虽如此,到目前为止我还没有找到真正的解决方案。也许您可以将自定义 inputAccessoryViewController 附加到 SearchController 并以这种方式进行更改?

抱歉,我不能提供更多帮助。

对于所有想要在 tvOS 上使用 TabBarController 和故事板实现此目的的人,我所做的是将容器视图添加到我的视图控制器并将选项卡栏控制器添加为子项。

class SearchController: UIViewController  {

    @IBOutlet var containerView: UIView!


    override func viewDidLoad() {
        super.viewDidLoad()

        let searchController = UISearchController.init(searchResultsController: nil)
        searchController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        searchController.view.frame = containerView.bounds        

        let searchContainer: UISearchContainerViewController = UISearchContainerViewController(searchController: searchController)

        // Finally contain the `UISearchContainerViewController` in a `UINavigationController`.
        let searchNavigationController = UINavigationController(rootViewController: searchContainer)
        searchNavigationController.navigationBar.isTranslucent = true
        searchNavigationController.navigationBar.tintColor = .black
        searchNavigationController.tabBarItem.title = "Search"

        containerView.addSubview(searchNavigationController.view)
        searchNavigationController.didMove(toParent: self)
    }
}