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

标签 ios swift scope uisearchbar uisegmentedcontrol

Swift 3,我正在尝试将搜索栏文本颜色从蓝色更改为黑色。例如“取消”和范围栏蓝色文本和边框颜色是蓝色,我希望它是黑色的。

这就是我的。

pic1

我试过这一行,但我知道的不够多,正如你所看到的,这条线不起作用。

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

其余的搜索条码

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

但是scope bar还是蓝色的,需要是黑色

pic3

更新图片 pic4

最佳答案

你遗漏了一些东西,这是为你的搜索栏做的方法:

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)

关于ios - 如何更改搜索栏的范围颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45929804/

相关文章:

ios - 两个单独的 cookie 存储(UIWebView 或 WKWebView)

javascript - 请求 promise 总是返回响应而不是返回值

ios - 为 NSCalendarIdentifier 返回 nil 使用变量属性值

ios - UILabel 在 3.5 英寸 iPhone 4 屏幕上被截断?

ios - 生成新的 UITableViewController 并显示它

ios - Swift 协议(protocol)定义了一个不适用于 UIViewController 的 init

ios - 更改数据源时 UICollectionView 中的平滑动画

ios - AVFoundation 播放声音 swift 2

javascript - 了解常见的 jQuery 插件样板

c++ - 形参1类型不完整错误