在iOS 7联系人应用中创建一个搜索栏

时间:2022-09-10 19:22:47

I have an application where I have a UITableView with sections which are scrollable on the side through sectionIndexTitlesForTableView:.

我有一个应用程序,我有一个UITableView的部分可以通过sectionIndexTitlesForTableView:侧面滚动。

In the tableHeaderView of the UITableView I want to add a UISearchBar, which I want to behave like the one Apple is using in their new Contacts app in iOS7. But I can't seem to get it to work.

在UITableView的tableHeaderView中,我想添加一个UISearchBar,我希望它像Apple在iOS7中的新Contacts应用程序中使用的那样。但我似乎无法让它发挥作用。

The search bar is not 100% the width of the screen, but instead has its right side against the section index.

搜索栏不是屏幕宽度的100%,而是右侧与节索引相对应。

My questions are:

我的问题是:

  • How do I get the search bar's width to 100% when it's the tableHeaderView of a UITableView that has a visible section index?
  • 当具有可见部分索引的UITableView的tableHeaderView时,如何将搜索栏的宽度设置为100%?
  • How can I create a transition like in the Contacts app of iOS 7 where the navigation bar hides and the search bar's grey background extends to the status bar?
  • 如何在iOS 7的“联系人”应用程序中创建转换,其中导航栏隐藏且搜索栏的灰色背景延伸到状态栏?

I have tried several things already, including adding the search bar in the navigation bar, and using UISearchBarController, but I can't find good documentation on the Apple website on how to create this. Also, the transition guide from iOS6 to iOS7 has been no good help for me.

我已经尝试了几件事,包括在导航栏中添加搜索栏,以及使用UISearchBarController,但我在Apple网站上找不到关于如何创建它的好文档。此外,从iOS6到iOS7的过渡指南对我来说也没有什么帮助。

Here are two pictures that illustrate my problem: 在iOS 7联系人应用中创建一个搜索栏在iOS 7联系人应用中创建一个搜索栏

这是两张说明我问题的图片:

2 个解决方案

#1


5  

Two main differences to the way Apple do their app, which will solve your problems.

与苹果公司的应用方式有两个主要区别,这将解决您的问题。

First, their UISearchBar isn't the headerView for the tableView. It's a separate view above the table. You'll need to change your UITableViewController to a UIViewController that has a UITableView positioned just below your UISearchBar.

首先,他们的UISearchBar不是tableView的headerView。这是表格上方的单独视图。您需要将UITableViewController更改为UIViewController,其UITableView位于UISearchBar下方。

Second, their UISearchBar has an associated Search Display Controller. Assuming you're using a storyboard file, this comes bundled together for you - just add the UISearchBar with the Search Display Controller. This will handle all of the transitioning to underneath the status bar for you.

其次,他们的UISearchBar有一个相关的搜索显示控制器。假设您正在使用故事板文件,这将捆绑在一起 - 只需将UISearchBar添加到搜索显示控制器即可。这将处理状态栏下方的所有转换。

#2


0  

Two things:

两件事情:

1) Set tableView's sectionIndexBackgroundColor to [UIColor clearColor].

1)将tableView的sectionIndexBackgroundColor设置为[UIColor clearColor]。

2) Implement 'viewDidLayoutSubviews' method to fix frame for UISearchBar

2)实现'viewDidLayoutSubviews'方法来修复UISearchBar的框架

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  CGRect barFrame = self.searchBar.frame;
  barFrame.size.width = self.view.bounds.size.width;
  self.searchBar.frame = barFrame;
}

#1


5  

Two main differences to the way Apple do their app, which will solve your problems.

与苹果公司的应用方式有两个主要区别,这将解决您的问题。

First, their UISearchBar isn't the headerView for the tableView. It's a separate view above the table. You'll need to change your UITableViewController to a UIViewController that has a UITableView positioned just below your UISearchBar.

首先,他们的UISearchBar不是tableView的headerView。这是表格上方的单独视图。您需要将UITableViewController更改为UIViewController,其UITableView位于UISearchBar下方。

Second, their UISearchBar has an associated Search Display Controller. Assuming you're using a storyboard file, this comes bundled together for you - just add the UISearchBar with the Search Display Controller. This will handle all of the transitioning to underneath the status bar for you.

其次,他们的UISearchBar有一个相关的搜索显示控制器。假设您正在使用故事板文件,这将捆绑在一起 - 只需将UISearchBar添加到搜索显示控制器即可。这将处理状态栏下方的所有转换。

#2


0  

Two things:

两件事情:

1) Set tableView's sectionIndexBackgroundColor to [UIColor clearColor].

1)将tableView的sectionIndexBackgroundColor设置为[UIColor clearColor]。

2) Implement 'viewDidLayoutSubviews' method to fix frame for UISearchBar

2)实现'viewDidLayoutSubviews'方法来修复UISearchBar的框架

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  CGRect barFrame = self.searchBar.frame;
  barFrame.size.width = self.view.bounds.size.width;
  self.searchBar.frame = barFrame;
}