-1

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

最简单的。

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

结果:

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

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

让我知道。:)

4

1 回答 1

0

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

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

如果您需要在用户在搜索栏中键入内容时更新标题,请使用updateSearchResults. UISearchResultsUpdatinghttps://developer.apple.com/documentation/uikit/uisearchresultsupdating/1618658-updatesearchresults

func updateSearchResults(for searchController: UISearchController) {
    self.navigationItem.title = searchController.searchBar.text ?? "Search Title"
}
于 2020-09-15T10:48:06.270 回答