簡體   English   中英

如何在導航欄標題文本上設置搜索欄的文本?

[英]How to set Search Bar's text on Navigation Bar Title Text?

我正在構思一個新的應用程序。 我喜歡找到很多技巧。 然而,我沒有運氣。

簡單的。

您可以看到從導航欄下方向下滑動搜索欄。

結果:

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

現在,我試圖尋找技巧代碼來設置搜索欄的文本在您搜索它時會出現導航欄。 (是的,這是一個非常熟悉的 Safari 風格)。

我不喜歡搜索欄標題文本停留在搜索欄的小部分,所以移到大標題看起來更好。

讓我知道。 :)

您可以使用UISearchBarDelegate searchBarSearchButtonClicked方法。 ( https://developer.apple.com/documentation/uikit/uisearchbardelegate/1624294-searchbarsearchbuttonclicked )

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    self.navigationItem.title = searchBar.text ?? "Search Title"
}

如果您需要在用戶在搜索欄中鍵入時更新標題,請使用UISearchResultsUpdating 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM