rpl*_*orn 36 cancel-button uisearchbar ios4
我已经将UISearchBar实现到一个表视图中,除了一件小事之外几乎所有东西都在工作:当我输入文本然后按键盘上的搜索按钮时,键盘消失,搜索结果是表中显示的唯一项目,文本保留在UISearchBar中,但取消按钮被禁用.
我一直试图让我的列表接近Apple联系人应用程序的功能,当你在该应用程序中按搜索时,它不会禁用取消按钮.
当我查看UISearchBar头文件时,我注意到_searchBarFlags结构下的autoDisableCancelButton标志,但它是私有的.
设置UISearchBar时是否有一些我遗漏的东西?
rpl*_*orn 51
我找到了解决方案.您可以使用此for循环遍历搜索栏的子视图,并在键盘上按下搜索按钮时启用它.
for (UIView *possibleButton in searchBar.subviews)
{
if ([possibleButton isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton*)possibleButton;
cancelButton.enabled = YES;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*las 15
我不得不调整一下,以便在iOS7中为我工作
- (void)enableCancelButton:(UISearchBar *)searchBar
{
for (UIView *view in searchBar.subviews)
{
for (id subview in view.subviews)
{
if ( [subview isKindOfClass:[UIButton class]] )
{
[subview setEnabled:YES];
NSLog(@"enableCancelButton");
return;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有两种方法可以轻松实现这一目标
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
// The small and dirty
[(UIButton*)[searchBar valueForKey:@"_cancelButton"] setEnabled:YES];
// The long and safe
UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
if ([cancelButton respondsToSelector:@selector(setEnabled:)]) {
cancelButton.enabled = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该使用第二个,如果Apple将在后台更改它,它不会使您的应用程序崩溃.
BTW我测试它从iOS 4.0到8.2并没有变化,我也在我的商店批准的应用程序中使用它没有任何问题.
| 归档时间: |
|
| 查看次数: |
15415 次 |
| 最近记录: |