gpt4 book ai didi

swift - 更改 UISearchController View 的样式?

转载 作者:可可西里 更新时间:2023-10-31 23:13:48 25 4
gpt4 key购买 nike

问题:

如何在 tvOS 中更改 UISearchController 的颜色/主题/样式?

(看来我需要以某种方式设置 UIStatusBarStyle,因为 tvOS 中不存在 UISearchControllerpreferredStatusBarStyle 覆盖)

描述:

在我的 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

}

上述方法的可视化表示图像:

UISearchViewController

我尝试过的:

我曾尝试通过不同的方法来改变样式,但没有一个能达到预期的结果。

这对搜索栏没有影响:

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

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

searchController.searchBar.backgroundColor = .black

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

searchController.view.backgroundColor = .black

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

尝试实现 this solution :

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

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

最佳答案

对于所有希望在 tvOS 上使用 TabBarController 和 Storyboard实现此目的的人,我所做的是将容器 View 添加到我的 View Controller 并将选项卡栏 Controller 添加为子项。

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

关于swift - 更改 UISearchController View 的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40991845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com