重新创建Quora应用程序iOS 7 UISearchBar

重新创建Quora应用程序iOS 7 UISearchBar,ios,iphone,ios7,uisearchbar,uisearchdisplaycontroller,Ios,Iphone,Ios7,Uisearchbar,Uisearchdisplaycontroller,Quora的iOS 7应用程序似乎在主UINavigationController的UINavigationBar中显示一个UISearchBar。点击该栏中的右栏按钮项可触发搜索,并从右侧设置动画。一切似乎都发生在一个视图控制器中 苹果在iOS 7中向UISearchDisplayController引入了“displaysSearchBarInNavigationBar”属性,但我不相信Quora使用过这个。设置时不可能在导航栏中显示标题视图,Quora应用程序显然有此功能 苹果的日历应用程

Quora的iOS 7应用程序似乎在主UINavigationController的UINavigationBar中显示一个UISearchBar。点击该栏中的右栏按钮项可触发搜索,并从右侧设置动画。一切似乎都发生在一个视图控制器中

苹果在iOS 7中向UISearchDisplayController引入了“displaysSearchBarInNavigationBar”属性,但我不相信Quora使用过这个。设置时不可能在导航栏中显示标题视图,Quora应用程序显然有此功能

苹果的日历应用程序使用一个单独的视图控制器进行搜索,但像Quora一样,将搜索保持在与内容相同的视图控制器中,对用户来说是一种更好的体验


我认为实现这一点的唯一方法可能是使用自定义UIViewController转换,但这感觉有点过头了。有没有更简单的方法来创建它?

这是我编写的一个快速示例视图控制器,模拟Quora的“扩展搜索字段”效果。我将留给您合并“搜索结果”表视图

我使用了一个
UITextField
(子类化以便我可以更改占位符文本的颜色…),但您可能可以使用
UISearchBar
。或者,您可能希望创建一个包含
UITextField
和任何“关闭”按钮的自定义视图。在我的示例中,我使用
UITextField
rightView来保持关闭按钮;在Quora案例中,它们在文本字段外有关闭按钮,就像真实的
UISearchBar
。但我认为您无法更改
ui搜索栏上关闭按钮的文本/图像(至少不容易)

您可能会想出一个干净地集成了内置searchViewController的解决方案,但这可能会给它带来太多的麻烦

@接口TSTextField:UITextField
@结束
@实现TSTextField
-(void)drawPlaceholderInRect:(CGRect)rect
{
CGFloat fontHeight=self.font.lineHeight;
CGFloat yOffset=(rect.size.height-fontHeight)/2.0;
rect=CGRectMake(0,yOffset,rect.size.width,fontHeight);
[[self占位符]drawInRect:rect
withAttributes:@{NSFontAttributeName:self.font,
NSForegroundColorAttributeName:self.isEditing?self.textColor:[UIColor whiteColor]}];
}
@结束
@接口TSViewController()
@结束
@TSViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.navigationController.navigationBar.barTintColor=[UIColor redColor];
UITextField*searchField=[[TSTextField alloc]initWithFrame:CGRectMake(0,0,85,30)];
searchField.backgroundColor=[UIColor clearColor];
searchField.borderStyle=UITextBorderStyleNone;
searchField.textColor=[UIColor-whiteColor];
searchField.textAlignment=NSTextAlignmentRight;
searchField.placeholder=@“搜索”;
searchField.delegate=self;
UIButton*放大按钮=[UIButton按钮类型:UIButtonTypeSystem];

[放大按钮setTitle:@“您所说的”是什么意思?设置时,导航栏中不可能有标题视图,Quora应用程序显然有此功能。"?从文档中,我的理解是,设置displaysSearchBarInNavigationBar会导致使用搜索显示控制器的导航项,而导航项不能有标题。这很好,谢谢。我以前没有使用transitionWithView,这似乎是关键。正如@TomSwift提到的,r需要做一些额外的工作但这是一个奇妙的答案。