如何将UISearchBar上"Cancel"按钮改为”取消“

 我来答
丁丹秋
2016-09-22 · 超过70用户采纳过TA的回答
知道小有建树答主
回答量:89
采纳率:0%
帮助的人:30.8万
展开全部
1 修改背景
searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(0,KTopBarHeight, 320, KTopBarHeight)];
searchbar.delegate=self;
searchbar.keyboardType = UIKeyboardTypeDefault;
//[[searchbar.subviews objectAtIndex:0]removeFromSuperview];
UIImage *img = [[UIImage imageNamed: @"searchBar_bg.png"]stretchableImageWithLeftCapWidth:0 topCapHeight:22];
UIImageView *v = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[v setImage:img];
v.bounds = CGRectMake(0, 0, searchbar.frame.size.width, searchbar.frame.size.height);

NSArray *subs = searchbar.subviews;
for (int i = 0; i < [subs count]; i++) {
id subv = [searchbar.subviews objectAtIndex:i];
if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
CGRect viewRect = [subv frame];
[v setFrame:viewRect];
[searchbar insertSubview:v atIndex:i];
}
}

searchbar.placeholder=@"请输入疾病名,症状或问题";
2 修改cancel按钮

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
searchBar.showsCancelButton=YES;
btn.hidden=NO;
searchBar.showsCancelButton = YES;
for(id cc in [searchBar subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *sbtn = (UIButton *)cc;
[sbtn setTitle:@"取消" forState:UIControlStateNormal];
[sbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式