Ios 更改色调颜色并不改变“颜色”的字体;取消“;搜索栏中的按钮
我有一个名为SearchView的UIView,我正在添加searchController.searchBar作为UIView的子视图 在viewDidLoad()中,我正在更改UISearchController中的搜索栏,如下所示:Ios 更改色调颜色并不改变“颜色”的字体;取消“;搜索栏中的按钮,ios,swift,uisearchcontroller,Ios,Swift,Uisearchcontroller,我有一个名为SearchView的UIView,我正在添加searchController.searchBar作为UIView的子视图 在viewDidLoad()中,我正在更改UISearchController中的搜索栏,如下所示: //makes background transparent searchController.searchBar.backgroundImage = UIImage() //makes magnifying glass white
//makes background transparent
searchController.searchBar.backgroundImage = UIImage()
//makes magnifying glass white
let iconView:UIImageView = tf!.leftView as! UIImageView
iconView.image = iconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
iconView.tintColor = UIColor.whiteColor()
//changes text color to white
let tf = searchController.searchBar.valueForKey("searchField") as? UITextField
let attributedString = NSAttributedString(string: "", attributes:[NSForegroundColorAttributeName : UIColor.whiteColor()])
tf!.attributedPlaceholder = attributedString
tf!.textColor = UIColor.whiteColor()
//changes search field background color
tf!.backgroundColor = UIColor(red: 82/255, green: 91/255, blue: 93/255, alpha: 1)
//HERE: should make the cancel button font white...
searchController.searchBar.tintColor = UIColor.whiteColor()
searchView.addSubview(searchController.searchBar)
在AppDelegate中,我有
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()
在启动时的应用程序委托中,说:
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()
请注意,这仅在搜索栏中正在进行编辑时才有效。如果搜索栏中没有编辑,则没有要取消的内容,因此“取消”按钮变暗且没有颜色(它是一种基于色调的灰色)
查看此检查苹果的示例,只需在viewDidLoad中添加searchController.searchBar.tintColor=UIColor.whiteColor()method@Pran我使用了(uibarbuttoneim.appearancewhencontainedInstancesofClass([UISearchBar.self]).tintColor=UIColor.whiteColor(),但这不起作用。穆罕默德,我试着自己做,然后我试着结合Pran的答案做,取消按钮的字体仍然不会改变。这太糟糕了。@Muhammaddnan正如你所见,我已经完成了searchController.searchBar.tintColor=UIColor.whiteColor(),但它不起作用。@Muhammaddnan在我希望背景透明时,最终将背景变成白色。另外,取消按钮文本仍然是浅灰色。谢谢你的回答,我相信这仍然会帮助一些人…但这仍然不会改变我的取消按钮字体。我不知道为什么我会被否决。我会用人们开始回答我的问题后我尝试过的方法来更新我的问题。很明显,我不知道你还在做什么。我的回答包括一个屏幕截图,显示它确实有效。正如我在回答中所解释的,“浅灰色”仅在您未在搜索栏中编辑时出现,对此您无能为力。编辑时,字体对我来说仍然不是白色。我发布了所有相关代码,所以我没有隐瞒任何事情。不确定发生了什么。uiBarButtonim.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor(红色:72/255,绿色:79/255,蓝色:79/255,alpha:1),NSFontAttributeName:UIFont(名称:“Open SAN”,大小:14)!],对于状态:UIControlState.Normal),我认为您的代码会覆盖之前放置的代码。我把这段代码注释掉了,你的代码运行得很好。对不起,大家…没想到要检查AppDelegate。