我需要一个带有取消按钮的完全透明的搜索栏.我尝试了很多解决方案但是我找不到更好的解决方案.当我尝试删除背景颜色时,它会显示范围栏.任何人都可以给我一些带有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]; …Run Code Online (Sandbox Code Playgroud) 我已经实现了搜索栏,一旦用户在搜索栏中有焦点,就会显示取消按钮.为此我已经用searchBar.showsCancelButton = YES;我的searchBarTextDidBeginEditing方法写了.在searchBarSearchButtonClicked,我重新签名键盘,以便用户可以查看完整的tableview.
问题:此时搜索栏取消按钮没有响应.它只会在搜索栏再次获得焦点时响应.这是搜索栏取消按钮的默认属性还是我在代码中遗漏了一些东西.我想使用取消按钮而不再在搜索栏中给焦点.
我想从UISearchBar中删除清除按钮(灰色x)。我试图按照此答案中的说明进行操作,但是它不起作用。
我将Objective-C代码从下面的答案和注释翻译为以下Swift代码:
for subview in searchBar.subviews {
configureSearchBarView(subview as UIView)
}
func configureSearchBarView(view: UIView) {
for subview in view.subviews {
self.configureSearchBarView(subview as UIView)
}
if subview.conformsToProtocol(UITextInputTraits) {
var clearView = view as UITextField
clearView.clearButtonMode = UITextFieldViewMode.Never
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不会删除清除按钮。
另一个答案建议与使用appearanceWhenContainedIn,但是此方法似乎未在Swift中实现。
有任何想法吗?