jjzjj

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

coder 2024-01-20 原文

Quora 的 iOS 7 应用似乎在主 UINavigationController 的 UINavigationBar 中显示了一个 UISearchBar。搜索是通过点击该栏中的右侧栏按钮项目触发的,并从右侧开始动画。一切似乎都发生在单个 View Controller 中。

Apple 在 iOS 7 中向 UISearchDisplayController 引入了“displaysSearchBarInNavigationBar”属性,但我认为 Quora 没有使用它。设置后导航栏中不可能有 titleView,而且 Quora 应用程序肯定有这个。

Apple 的日历应用程序使用单独的 View Controller 进行搜索,但将搜索与内容保持在同一个 View Controller 中(如 Quora 所做的那样)对用户来说是一种更好的体验。

我认为执行此操作的唯一方法可能是使用自定义 UIViewController 转换,但这感觉有点矫枉过正。有没有更简单的方法来创建它?

最佳答案

这是我编写的一个快速示例 View Controller ,用于模仿 Quora 的“扩展搜索字段”效果。我将由您来合并“搜索结果”表格 View 。

我使用了 UITextField(子类化以便我可以更改占位符文本的颜色...),但您可能可以使用 UISearchBar。或者您可能想要制作一个包含 UITextField 和任何“关闭”按钮的自定义 View 。在我的示例中,我使用 UITextField rightView 来按住关闭按钮;在 Quora 案例中,它们在文本字段外有关闭按钮,就像一个真正的 UISearchBar。但我认为您不能更改 UISearchBar 上关闭按钮的文本/图像(至少不容易)。

您可能会想出一个完全集成内置 searchViewController 的解决方案,但它的值(value)可能太麻烦了。

@interface TSTextField : UITextField
@end
@implementation 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 placeholder] drawInRect: rect
                    withAttributes: @{ NSFontAttributeName : self.font,
                                       NSForegroundColorAttributeName : self.isEditing ? self.textColor : [UIColor whiteColor] }];
}
@end

@interface TSViewController () <UITextFieldDelegate>
@end

@implementation TSViewController

- (void) viewDidLoad
{
    [super 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 = @"Search";
    searchField.delegate = self;

    UIButton* magnifyButton = [UIButton buttonWithType: UIButtonTypeSystem];
    [magnifyButton setTitle: @"?" forState: UIControlStateNormal];
    [magnifyButton sizeToFit];
    [magnifyButton addTarget: self action: @selector( close: ) forControlEvents: UIControlEventTouchUpInside];
    searchField.leftView = magnifyButton;
    searchField.leftViewMode = UITextFieldViewModeAlways;

    UIButton* closeButton = [UIButton buttonWithType: UIButtonTypeSystem];
    [closeButton setTitle: @"ⓧ" forState: UIControlStateNormal];
    [closeButton sizeToFit];
    [closeButton addTarget: self action: @selector( close: ) forControlEvents: UIControlEventTouchUpInside];
    searchField.rightView = closeButton;
    searchField.rightViewMode = UITextFieldViewModeWhileEditing;

    UIBarButtonItem* bbi = [[UIBarButtonItem alloc] initWithCustomView: searchField];
    self.navigationItem.rightBarButtonItem = bbi;

}

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    UITextField* searchField = (UITextField*)self.navigationItem.rightBarButtonItem.customView;
    searchField.borderStyle = UITextBorderStyleRoundedRect;
    searchField.backgroundColor = [UIColor whiteColor];
    searchField.textColor = [UIColor blackColor];
    searchField.text = @"";
    searchField.textAlignment = NSTextAlignmentLeft;

    [UIView transitionWithView: searchField
                      duration: 0.25
                       options: UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{

                        searchField.frame = CGRectMake( 0, 0, 290, searchField.frame.size.height);
                    }
                    completion: nil];

    return YES;
}

- (void) close: (id) sender
{
    UITextField* searchField = (UITextField*)self.navigationItem.rightBarButtonItem.customView;
    searchField.rightViewMode = UITextFieldViewModeNever;

    [UIView transitionWithView: searchField
                      duration: 0.25
                       options: UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{

                        searchField.frame = CGRectMake( 0, 0, 85, searchField.frame.size.height);
                        searchField.backgroundColor = [UIColor clearColor];
                        searchField.text = @"";
                        searchField.borderStyle = UITextBorderStyleNone;
                        searchField.textColor = [UIColor whiteColor];
                        searchField.textAlignment = NSTextAlignmentRight;

                        [searchField resignFirstResponder];
                     }

                     completion:^(BOOL finished) {

                         searchField.rightViewMode = UITextFieldViewModeWhileEditing;
                     }];
}

@end

关于ios - 重新创建 Quora 应用程序 iOS 7 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19961245/

有关ios - 重新创建 Quora 应用程序 iOS 7 UISearchBar的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  8. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  9. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

  10. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

随机推荐