133*_*ode 6 iphone uitextfield uisearchbar ipad ios
我试过了:
UITextField *searchtextfield = [searchBar.subviews objectAtIndex:1];
UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
cButton.frame = CGRectMake(0, 0, 20 , 20);
cButton.backgroundColor = [UIColor clearColor];
[cButton setImage:[UIImage imageNamed:@"x-button"] forState:UIControlStateNormal];//your button image.
cButton.contentMode = UIViewContentModeScaleToFill;
[cButton addTarget:self action:@selector(xButtonPressed) forControlEvents:UIControlEventTouchUpInside];//This is the custom event
[searchtextfield setRightView:cButton];
[searchtextfield setRightViewMode:UITextFieldViewModeWhileEditing];
Run Code Online (Sandbox Code Playgroud)
但它不能很好地工作......它显示自定义按钮,但是当你输入一些东西时,旧的东西会回来......
我怎样才能做到这一点?
小智 8
如果要在UISearchBar中设置自定义清除按钮,请尝试以下操作:
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"MyClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
不要忘记为其设置图像 UIControlStateHighlighted
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"HighlightedClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
Run Code Online (Sandbox Code Playgroud)
您需要以下内容
[searchBar setImage:[UIImage imageNamed:@"image1"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[searchBar setImage:[UIImage imageNamed:@"image2"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
强烈建议从UIControlStateHighlighted开始按此顺序放置字符串, 以防您想使用相同的图像:image1 = image2 = image.
在iOS7中,很奇怪,但事实是UIControlStateNormal和UIControlStateHighlighted的直接顺序不起作用.
您可以隐藏取消按钮searchBarTextDidBeginEditing
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
最令人惊奇的是,您还可以隐藏清除按钮
UITextField *textField=(UITextField*)[[searchBar subviews]objectAtIndex:1];
textField.clearButtonMode=UITextFieldViewModeNever;
Run Code Online (Sandbox Code Playgroud)
请关注我的答案,了解更多信息链接
| 归档时间: |
|
| 查看次数: |
8444 次 |
| 最近记录: |