iOS - 自定义UISearchBar的取消按钮 [英] iOS - Customizing Cancel button of UISearchBar
问题描述
在我的iOS5 iPhone应用程序中,我使用以下代码设置搜索栏的色调:
In my iOS5 iPhone application, i'm setting the tint color of search bar using following code:
searchBar.tintColor = UIColorMake(@"#EFEFEF");
#efefef的RGB值是(239,239,239)
工作正常。但是当出现取消按钮时,文本取消不可见。我可以自定义带有透明黑白文本的取消按钮吗?
是否可以自定义?
RGB value of #efefef is (239,239,239)
Its working fine. But when cancel button appears the text "Cancel" is not visible. Can I customize only the cancel button with transparent black and white text on that?
is it possible to customize?
推荐答案
您可以搜索 UISearchBar
subViews并找到取消按钮,这样做很危险,因为按钮可以更改
例如你可以在你的 viewWillAppear
You could search for UISearchBar
subViews and locate the cancel button, it is dangerous to do so, since the button could change
For example you could add this in your viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
//show the cancel button in your search bar
searchBar.showsCancelButton = YES;
//Iterate the searchbar sub views
for (UIView *subView in searchBar.subviews) {
//Find the button
if([subView isKindOfClass:[UIButton class]])
{
//Change its properties
UIButton *cancelButton = (UIButton *)[sb.subviews lastObject];
cancelButton.titleLabel.text = @"Changed";
}
}
}
正如我之前所说的那样改变,这是一个黑客,你最好坚持原来,或创建自己的搜索栏。
As i said before this could change, its a hack to do so, you better stick with the original, or create your own search bar.
这篇关于iOS - 自定义UISearchBar的取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!