【问题标题】:How to set Search Bar's text on Navigation Bar Title Text?如何在导航栏标题文本上设置搜索栏的文本?
【发布时间】:2021-01-01 18:01:03
【问题描述】:

我正在构思一款新应用。我喜欢找到很多技巧。但是,我没有运气。

简单。

您可以看到从导航栏下方向下滑动搜索栏。

结果:

    // 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 非常熟悉的风格)。

我不喜欢搜索栏标题文本停留在较小的搜索栏上,所以移到较大的标题会更好看。

告诉我。 :)

【问题讨论】:

    标签: ios swift search uinavigationcontroller uisearchbar


    【解决方案1】:

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

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

    如果您需要在用户在搜索栏中输入内容时更新标题,请使用updateSearchResultsUISearchResultsUpdating。 (https://developer.apple.com/documentation/uikit/uisearchresultsupdating/1618658-updatesearchresults)

    func updateSearchResults(for searchController: UISearchController) {
        self.navigationItem.title = searchController.searchBar.text ?? "Search Title"
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-12
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多