2017-08-29 6 views
0

Swift 3, ich versuche, die Suchleiste Textfarbe von blau nach schwarz zu ändern. Zum Beispiel die "Abbrechen" und die Scope Bars blauen Text und Rahmenfarbe ist blau, ich will es schwarz.Wie ändere ich die Farben des Suchbereichs?

Das ist was ich habe.

pic1

Und habe ich versucht, diese Zeile aber ich weiß nicht genug, und diese Linie nicht funktioniert, wie Sie sehen können.

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 

Rest SearchBar Code

// 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() 
    } 
} 

neuen 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) 

Aber der Umfang Bar ist immer noch blau, und es muss pic4

Antwort

1

ich bin fehlt ein paar Dinge schwarz

pic3

aktualisiertes Bild sein, hier ist so, wie es für Ihre Suche zu tun Bar:

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

Und für Ihre segmentierte Kontrolle:

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

Dies funktionierte für die Abbrechen-Schaltfläche, aber nicht die Bereichsleiste, es ist immer noch blau – BroSimple

+0

@BroSimple, überprüfen Sie das Update, um zu sehen, wie Sie die Textfarbe Ihres segmentierten Steuerelements ändern. –

+0

danke! aber jetzt ändert sich nur der Text in den Scope-Bar-Buttons, es ist immer noch blau im Rahmen des Scopes und der ausgewählte Text hat immer noch das Blau, (im Bild habe ich das "All", das den blauen Hintergrund im ausgewählten Text hat) – BroSimple

Verwandte Themen