如何更改搜索栏的范围颜色?

How to change searchbar's scope colors?

Swift 3,我正在尝试将搜索栏文本颜色从蓝色更改为黑色。例如 "cancel" 和范围栏蓝色文本和边框颜色是蓝色,我想要它黑色。

这就是我的。

我试过这一行,但我不够了解,而且这一行不能像你看到的那样工作。

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

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

其余搜索条码

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

新代码:

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

但是范围栏还是蓝色的,需要是黑色的

更新图片

您遗漏了一些东西,这是为您的搜索栏执行此操作的方法:

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

对于您的分段控件:

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