我有一个UISearchBar,我为UiControlStateNormal设置了一个自定义的UISearchBarIconClear.
[mySearchBar setImage:myImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
这个部分可以正常工作但不幸的是,当点击清除按钮时,它会从我设置的图像变为原始的默认灰色按钮.
我已经尝试为UIControlStateHighlighted设置图像,但显然这不起作用.
该文件实际上规定
有效状态是UIControlStateNormal和UIControlStateDisabled.
如果您无法将其设置为突出显示状态,那么为默认状态设置自定义按钮的重点是什么?我错过了什么吗?任何想法或变通方法都表示赞赏,谢谢!
我试过了:
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)
但它不能很好地工作......它显示自定义按钮,但是当你输入一些东西时,旧的东西会回来......
我怎样才能做到这一点?