如何更改UISearchController
中tvOS
的颜色/主题/风格?
(看来我需要以某种方式设置UIStatusBarStyle,因为tvOS
UISearchController
中的AppDelegate
覆盖不存在
在我的searchNavigationController
中调用的方法,以编程方式创建一个UISearchController
,其位于UITableViewController
的最上方,而我的自定义searchResultsController
名为“SearchViewController
”({ {1}})
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
}
上述方法的直观表示形象:
我试图通过不同的方法改变风格,但没有一种方法能达到预期的效果。
这对searchBar没有影响:
searchController.searchBar.searchBarStyle = .minimal //or .prominent or .default
这只会使searchBar(实际输入区域为黑色..带黑色文字..):
searchController.searchBar.backgroundColor = .black
这只会使UISearchController的整个背景视图变黑。一切都是黑色的,因此无法看到键盘。
searchController.view.backgroundColor = .black
理想的解决方案是从标准.default
到.dark
的主题更改。因为不仅必须改变背景颜色,而且边框和文字颜色也必须改变。
//UISearchController
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
然而,tvOS似乎不存在这种覆盖。有什么理由吗?
答案 0 :(得分:3)
对于所有希望通过TabBarController和Storyboard在tvOS上实现此目标的人,我所做的就是将容器视图添加到我的视图控制器中,并将选项卡栏控制器作为子项添加。
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)
}
}
答案 1 :(得分:0)
有一个解决这个问题的方法,但是我真的不明白他们为什么不保持与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
}
答案 2 :(得分:0)
我不是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的这一声明中达到了高潮;
内联键盘在一行上显示所有可能的输入。使用UISearchController创建可与第三方内容完全集成的键盘。但是,使用UISearchController时,很少有自定义选项。您无法访问文本字段本身,自定义特征或添加输入附件。
我怀疑如果您能够获得对该键盘的引用,您可能会影响某些更改。话虽如此,到目前为止我还没有找到真正的解决方案。也许您可以将自定义inputAccessoryViewController附加到SearchController并以这种方式实现更改?
抱歉,我无法提供更多帮助。