[英]How to set Search Bar's text on Navigation Bar Title Text?
I am getting idea of a new app.我正在构思一个新的应用程序。 I love to find a many tricks.
我喜欢找到很多技巧。 however, No luck for me.
然而,我没有运气。
The simple.简单的。
You can see swipe down the search bar from the below of the navigation bar.您可以看到从导航栏下方向下滑动搜索栏。
Result:结果:
// iOS 13 Navigation Bar only
self.navigationItem.title = "Search Title"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationItem.largeTitleDisplayMode = .never
UINavigationBar.appearance().isTranslucent = false
let app = UINavigationBarAppearance()
let navigationBar = self.navigationController?.navigationBar
app.backgroundColor = .clear
app.configureWithOpaqueBackground()
app.titleTextAttributes = [.foregroundColor: UIColor.label]
app.largeTitleTextAttributes = [.foregroundColor: UIColor.label]
app.backgroundColor = .systemGroupedBackground
self.navigationController?.navigationBar.scrollEdgeAppearance = app
navigationBar!.standardAppearance = app
navigationBar!.scrollEdgeAppearance = app
Search Result:搜索结果:
// Search Bars
let search = UISearchController(searchResultsController: nil)
search.searchBar.delegate = self
search.searchResultsUpdater = self as? UISearchResultsUpdating
search.searchBar.placeholder = "Search"
search.searchBar.searchTextField.tintColor = UIColor.gray
search.searchBar.setImage(UIImage(named: "magnifyingglass")?.withTintColor(UIColor.systemGray), for: .search, state: .normal)
self.navigationItem.searchController = search
(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(white: 100, alpha: 0.50)]
let textField = search.searchBar.value(forKey: "searchField") as! UITextField
let glassIconView = textField.leftView as! UIImageView
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = UIColor.systemGray
let clearButton = textField.value(forKey: "clearButton") as! UIButton
clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
clearButton.tintColor = UIColor.systemGray
Now, I am tried to look for the trick code to set the search bar's text will appear navigation bar when you have searched it.现在,我试图寻找技巧代码来设置搜索栏的文本在您搜索它时会出现导航栏。 (Yes, It is a very familiar to Safari style).
(是的,这是一个非常熟悉的 Safari 风格)。
I don't like Search Bar title text stayed on the small of the search bars, so move to a large title look better.我不喜欢搜索栏标题文本停留在搜索栏的小部分,所以移到大标题看起来更好。
Let me know.让我知道。 :)
:)
You can use searchBarSearchButtonClicked
method of UISearchBarDelegate
.您可以使用
UISearchBarDelegate
searchBarSearchButtonClicked
方法。 ( https://developer.apple.com/documentation/uikit/uisearchbardelegate/1624294-searchbarsearchbuttonclicked ) ( https://developer.apple.com/documentation/uikit/uisearchbardelegate/1624294-searchbarsearchbuttonclicked )
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.navigationItem.title = searchBar.text ?? "Search Title"
}
If you need to update the title as the user types in search bar, use updateSearchResults
of UISearchResultsUpdating
.如果您需要在用户在搜索栏中键入时更新标题,请使用
UISearchResultsUpdating
updateSearchResults
。 ( https://developer.apple.com/documentation/uikit/uisearchresultsupdating/1618658-updatesearchresults ) ( https://developer.apple.com/documentation/uikit/uisearchresultsupdating/1618658-updatesearchresults )
func updateSearchResults(for searchController: UISearchController) {
self.navigationItem.title = searchController.searchBar.text ?? "Search Title"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.