我如何实际创建自定义UISearchBar?

时间:2017-12-21 01:49:34

标签: swift uitextfield uikit uisearchbar uicolor

我一直在墙上撞了一天,试图弄清楚如何创建一个自定义的UISearchBar,这样我就可以摆脱UIKit认为我想要的股票黑色搜索栏。我尝试了一些“解决方案”:

1。自定义AppDelegate中的外观

//default background color for search bars
UISearchBar.appearance(whenContainedInInstancesOf: [UISearchController.self]).tintColor = UIColor(hexString: "#394C53")

这不起作用

2。子类化UISearchControllerUISearchBar

我跟着this tutorial无法正常工作

第3。循环浏览子视图

let results = DestinationSearchResultsTableViewController()
results.mapView = map
results.handleMapSearchDelegate = self

destinationSearch = UISearchController(searchResultsController: results)
destinationSearch!.searchBar.placeholder = "Where do you want to go?"
if #available(iOS 11, *) {
    navigationItem.searchController = destinationSearch

    if let textfield = destinationSearch!.searchBar.value(forKey: "searchField") as? UITextField {
        if let backgroundview = textfield.subviews.first {
            // Below is not what I want but it works
            backgroundview.backgroundColor = UIColor.white
            backgroundview.layer.cornerRadius = 10
            backgroundview.clipsToBounds = true;
        }
    }
} else {   
    self.navigationController?.pushViewController(destinationSearch!, animated: true)
}

destinationSearch!.delegate = self
destinationSearch!.searchResultsUpdater = results as UISearchResultsUpdating

这实际上有点有效。如果我使用UIColor预定义的颜色,例如UIColor.white UIColor.red等,它的效果非常好。但如果我想使用这样的特定颜色,它的效果非常好:enter image description here我尝试使用上面颜色的RGB和十六进制表示,但我得到的只是黑色。

这就是我想要的:

我想创建我自己的UISearchBar,左边对齐的标签和右边的内容文本字段,如上所示。

由于

0 个答案:

没有答案
相关问题