相关疑难解决方法(0)

为UIControlStateHighlighted定制UISearchBarIconClear不起作用

我有一个UISearchBar,我为UiControlStateNormal设置了一个自定义的UISearchBarIconClear.

[mySearchBar setImage:myImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

这个部分可以正常工作但不幸的是,当点击清除按钮时,它会从我设置的图像变为原始的默认灰色按钮.

我已经尝试为UIControlStateHighlighted设置图像,但显然这不起作用.

文件实际上规定

有效状态是UIControlStateNormal和UIControlStateDisabled.

如果您无法将其设置为突出显示状态,那么为默认状态设置自定义按钮的重点是什么?我错过了什么吗?任何想法或变通方法都表示赞赏,谢谢!

objective-c uikit uisearchbar ios

7
推荐指数
2
解决办法
1831
查看次数

UISearchBar文本字段中的自定义清除按钮

我试过了:

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)

但它不能很好地工作......它显示自定义按钮,但是当你输入一些东西时,旧的东西会回来......

我怎样才能做到这一点?

iphone uitextfield uisearchbar ipad ios

6
推荐指数
3
解决办法
8444
查看次数

标签 统计

ios ×2

uisearchbar ×2

ipad ×1

iphone ×1

objective-c ×1

uikit ×1

uitextfield ×1