灰色 UISearchBar 以编程方式匹配范围栏

发布于 2024-09-04 10:54:50 字数 432 浏览 5 评论 0原文

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

alt text http://img168 .imageshack.us/img168/6378/43558113.png

我见过的所有示例都涉及使用 xib,但是我需要以编程方式执行此操作。问题是更改色调颜色也会更改取消按钮的色调:

alt text http://img243.imageshack.us/ img243/1375/screenshot20100527at944.png

有什么想法吗?

I'm trying to recreate this UISearchBar (as seen in the Table Search example code):

alt text http://img168.imageshack.us/img168/6378/43558113.png

All the examples I've seen to do this involve using a xib, however I need to do it programmatically. The problem is changing the tint color also changes the cancel button's tint:

alt text http://img243.imageshack.us/img243/1375/screenshot20100527at944.png

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-09-11 10:54:50

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

  • 灰色色调,而不影响取消按钮
  • 自动显示/隐藏
  • 任何表视图索引周围的取消按钮宽度调整

在我的表视图控制器中,我做了以下操作:

- (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];
}

Associating the search bar with a UISearchDisplayController magically provides a lot of standard look and behavior such as:

  • gray tint without affecting cancel button
  • auto showing/hiding of cancel button
  • width adjustment around any tableview indexes

In my tableview controller I've done the following:

- (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];
}
内心激荡 2024-09-11 10:54:50

我完全同意斯科特·麦卡蒙的观点。

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

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


- (UISearchDisplayControlelr *) searchDisplayController {
    return yourInstanceOfASearchController;
}

I totally agree with Scott McCammon.

However using a performSelector:withObject: on setSearchDisplayController: would not be my approach. This depends on private API which can change at any moment. If Apple would remove their private implementation your app will crash.

A better way would be to override the searchDisplayController: in your view controller to return your instance of UISearchDisplayController:


- (UISearchDisplayControlelr *) searchDisplayController {
    return yourInstanceOfASearchController;
}
相对绾红妆 2024-09-11 10:54:50

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

I don't understand the need for the call to setSearchDisplayController: or the override for searchDisplayController. Under iOS 4.3.2 initWithSearchBar:contentsController: appears to set searchDisplayController for the UIViewController instance passed as the contentsController argument. Perhaps this was a problem in earlier iOS releases, but it appears redundant in the current release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文