如何在UISearchbar中更改清除按钮图像(x按钮)或至少更改清除按钮图像的颜色?

时间:2012-10-30 16:26:39

标签: ios uibutton uisearchbar

我需要一个带有取消按钮的完全透明的搜索栏。我尝试了很多解决方案但是我找不到更好的解决方案。当我尝试删除背景颜色时,它会显示范围栏。任何人都可以给我一些带有can按钮的完全透明的搜索栏的源代码。  这是

addSearchbar.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
UITextField *sbTextField = (UITextField *)[self.addSearchbar.subviews lastObject];

for (UIView *subview in addSearchbar.subviews)
{
    NSLog(@"%@",subview);
    if ([subview isKindOfClass:[UITextField class]])
    {
        sbTextField = (UITextField *)subview;
        UIImage *image = [UIImage imageNamed: @"06_magnifying_glass.png"];
        UIImageView *iView = [[UIImageView alloc] initWithImage:image];
        iView.frame = CGRectMake(0, 0, 24, 24);
        sbTextField.leftView.frame = CGRectMake(0, 0, 24, 24);
        sbTextField.leftView = iView;
        [sbTextField.rightView removeFromSuperview];
        CGFloat myWidth = 24.0f;
        CGFloat myHeight = 24.0f;
        UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
        [myButton setImage:[UIImage imageNamed:@"clear.png"] forState:UIControlStateNormal];
        [myButton setImage:[UIImage imageNamed:@"clear.png"] forState:UIControlStateHighlighted];

        [myButton addTarget:self action:@selector(doClear:) forControlEvents:UIControlEventTouchUpInside];
        sbTextField.rightView = myButton;
        sbTextField.rightViewMode = UITextFieldViewModeWhileEditing;
        break;
    }
    if([subview isMemberOfClass:[UISegmentedControl class]])
    {
        UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
        scopeBar.tintColor =  [UIColor clearColor];
    }
}
[sbTextField removeFromSuperview];
[addSearchbar addSubview:sbTextField];
[addSearchbar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchBar.png"] forState:UIControlStateNormal];
CGRect sbFrame = self.addSearchbar.frame;
// Set the default height of a textfield
sbFrame.size.height = 31;

/* 8 is the top padding for textfield inside searchbar
 * You may need to add a variable to 8 according to your requirement.
 */
sbFrame.origin.y = 6+self.addSearchbar.frame.origin.y;
sbTextField.frame = sbFrame;
sbTextField.textColor = [UIColor lightGrayColor];
[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin];

需要一个完全透明的搜索栏,其中包含取消按钮和清除按钮,但没有范围栏。

提前致谢

4 个答案:

答案 0 :(得分:24)

从iOS 5.0开始,您可以执行以下操作:

UIImage *imgClear = [UIImage imageNamed:@"clear"];
[addSearchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

多数民众赞成。您可能还想重复UIControlStateHighlighted州的行。

你应该做的最后一件事是在搜索栏的子视图内挖掘。保证有一天会休息。使用适当的API来自定义任何控件。

答案 1 :(得分:4)

请参见先设置UIControlHighlighted状态图像,然后再设置UIControlStateNormal状态图像,否则可能会遇到clearIcon未设置为突出显示状态的问题。 (不确定为什么会出现这个问题。)

[_searchBar setImage:[UIImage imageNamed:@"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[_searchBar setImage:[UIImage imageNamed:@"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

答案 2 :(得分:2)

对于那些在Swift中尝试这个的人(我知道有人会来这里寻找这个......),你可以去:

    self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
    self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted)

确保您的Assets.xcassets文件夹中包含正确的图像。

答案 3 :(得分:0)

以下是在UISearchBar的UITextField中更改放大镜和清除按钮图标的方法 没有新图标

// Reference search display controller search bar's text field (in my case). 
UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"]; 

// Magnifying glass icon. 
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView; 
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
leftImageView.tintColor = [UIColor whiteColor]; 

   // Clear button 
UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"]; 
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
clearButton.tintColor = [UIColor whiteColor];