Iphone Gray UISearchBar w/以编程方式匹配范围栏

Iphone Gray UISearchBar w/以编程方式匹配范围栏,iphone,uisearchbar,tintcolor,Iphone,Uisearchbar,Tintcolor,我正在尝试重新创建此UISearchBar(如表搜索示例代码所示): 我看到的所有实现这一点的示例都涉及到使用xib,但是我需要通过编程来实现。问题是更改色调颜色也会更改“取消”按钮的色调: 有什么想法吗?将搜索栏与UISearchDisplayController关联会神奇地提供许多标准外观和行为,例如: 灰色色调,不影响取消按钮 自动显示/隐藏取消按钮 围绕任何tableview索引的宽度调整 在我的tableview控制器中,我执行了以下操作: - (void)viewDidLoa

我正在尝试重新创建此UISearchBar(如表搜索示例代码所示):

我看到的所有实现这一点的示例都涉及到使用xib,但是我需要通过编程来实现。问题是更改色调颜色也会更改“取消”按钮的色调:


有什么想法吗?

将搜索栏与UISearchDisplayController关联会神奇地提供许多标准外观和行为,例如:

  • 灰色色调,不影响取消按钮
  • 自动显示/隐藏取消按钮
  • 围绕任何tableview索引的宽度调整
在我的tableview控制器中,我执行了以下操作:

- (void)viewDidLoad {
    [super viewDidLoad];

    // setup searchBar and searchDisplayController

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    [searchBar sizeToFit];
    searchBar.delegate = self;
    searchBar.placeholder = @"Search";
    self.tableView.tableHeaderView = searchBar;

    UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    // The above assigns self.searchDisplayController, but without retaining.
    // Force the read-only property to be set and retained. 
    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDC];

    searchDC.delegate = self;
    searchDC.searchResultsDataSource = self;
    searchDC.searchResultsDelegate = self;

    [searchBar release];
    [searchDC release];
}
我完全同意斯科特·麦卡蒙的观点

但是,在
setSearchDisplayController:
上使用
PerformSelect:withObject:
将不是我的方法。这取决于可以随时更改的私有API。如果苹果删除他们的私有实现,你的应用程序将崩溃

更好的方法是覆盖视图控制器中的
searchDisplayController:
,以返回
UISearchDisplayController
的实例:


- (UISearchDisplayControlelr *) searchDisplayController {
    return yourInstanceOfASearchController;
}

我不理解调用
setSearchDisplayController:
或覆盖
searchDisplayController
的必要性。在iOS 4.3.2下,
initWithSearchBar:contentsController:
为作为
contentsController
参数传递的
UIViewController
实例设置
searchDisplayController
。这可能是早期iOS版本中的一个问题,但在当前版本中似乎是多余的。

请检查Joris Kluivers关于覆盖searchDisplayControllerIn的回答,如果有人想使用它,我的应用程序刚刚因为使用非公共API而被拒绝。setSearchDisplayController是罪魁祸首。我做了更多的测试。searchDisplayController正在被分配,但没有像上面Scott McCammon所指出的那样被保留。我没有调用未记录的API,而是将保留的引用保存在实例变量中,然后在dealloc.同上中释放它。我在.m文件中使用了3个私有实例变量:UISearchBar*searchBar;NSArray*过滤器列表;UISearchDisplayController*searchController;。在ARC下,我不认为你需要在“dealloc”中把它们消灭,但我不是专家。当组合iOS 4和ARC时,您需要消除弱引用,但常规(强)引用会自动消除。显然,最好的做法是为诸如UISearchDisplayController之类的对象使用属性,更不用说一般的大多数对象了。事实上,我无法让我的SDC工作,除非我声明了一个属性。所以我使用了Scott McCammon的代码,并没有在本地声明SDC,而是为它创建了一个私有属性。