2017-08-29 2 views
0

Swift 3, je suis en train de changer la couleur du texte des barres de recherche du bleu au noir. Par exemple le "annuler" et les barres de portée bleu et la couleur de la bordure est bleu, je le veux noir.Comment changer les couleurs de la barre de recherche?

C'est ce que j'ai.

pic1

Et j'ai essayé cette ligne, mais je ne sais pas assez et cette ligne ne fonctionne pas comme vous pouvez le voir.

searchController.searchBar.setScopeBarButtonTitleTextAttributes([NSBackgroundColorAttributeName: UIColor.black], for: UIControlState.normal) 

pic2

viewDidLoad

// Search Bar 
searchController.searchResultsUpdater = self 
searchController.dimsBackgroundDuringPresentation = false 
definesPresentationContext = true 
myTableView.tableHeaderView = searchController.searchBar 

// Search Bar Border 
let searchBar = searchController.searchBar 
searchBar.backgroundImage = UIImage() 

// Scope Bar 
searchController.searchBar.scopeButtonTitles = ["All", "Released", "Unreleased", "Open Beta"] 
searchController.searchBar.delegate = self 

reste du Code SearchBar

// SEARCH BAR: Filtering Content 
func filterContentForSearchText(searchText: String, scope: String = "All") { 

    filteredFollowedArray = followedArray.filter { Blog in 

     let categoryMatch = (scope == "All") || (Blog.blogType == scope) 

     return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) 
    } 

    filteredBlogArray = blogArray.filter { Blog in 

     let categoryMatch = (scope == "All") || (Blog.blogType == scope) 

     return categoryMatch && (Blog.blogName.lowercased().contains(searchText.lowercased())) 
    } 

    myTableView.reloadData() 
} 

// SEARCH BAR: Updating Results 
func updateSearchResults(for searchController: UISearchController) { 

    filterContentForSearchText(searchText: searchController.searchBar.text!) 
} 

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {} 

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {} 

// SEARCH BAR: Scope 
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 

    filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) 
} 

// SEARCH BAR: Updating Scope 
func updateSearchResultsForSearchController(searchController: UISearchController) { 

    let searchBar = searchController.searchBar 
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] 

    filterContentForSearchText(searchText: searchController.searchBar.text!, scope: scope) 
} 

// Deallocating Search Bar 
deinit{ 
    if let superView = searchController.view.superview { 
     superView.removeFromSuperview() 
    } 
} 

nouveau code:

// Search Bar 
searchController.searchBar.backgroundColor = UIColor.white 
searchController.searchBar.barTintColor = UIColor.white 

// Coloring SearchBar Cancel button 
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.black] 
    UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal) 

// Scope: Selected text 
let titleTextAttributesSelected = [NSForegroundColorAttributeName: UIColor.white] 
    UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesSelected, for: .selected) 

// Scope: Normal text 
let titleTextAttributesNormal = [NSForegroundColorAttributeName: UIColor.black] 
    UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesNormal, for: .normal) 

Mais la barre est toujours portée bleu et il doit être noir

pic3

image mise à jour pic4

Répondre

1

manque quelques Trovit facilite les choses, voici la façon de le faire pour votre recherche bar:

let cancelButtonAttributes: NSDictionary =[NSForegroundColorAttributeName: UIColor.black] 
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal) 

Et pour votre contrôle segmentée:

// Selected text 
let titleTextAttributesSelected = [NSForegroundColorAttributeName: UIColor.green] 
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesSelected, for: .selected) 

// Normal text 
let titleTextAttributesNormal = [NSForegroundColorAttributeName: UIColor.black] 
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributesNormal, for: .normal) 
+0

cela a fonctionné pour le bouton d'annulation, mais pas la barre de portée, il est toujours – BroSimple

+0

bleu @BroSimple, vérifiez la mise à jour pour voir comment changer la couleur du texte de votre commande segmentée. –

+0

merci! mais maintenant tout le texte dans les boutons de la barre de champ changé, il y a encore bleu dans la limite de la portée et le texte sélectionné a encore le bleu, (dans le pic j'ai le « All » qui a le fond bleu dans le texte sélectionné – BroSimple