如何显示和隐藏 UISearchDisplayController 的 UISearchBar

2023-11-30

我有一个位于导航右侧的按钮搜索。 这是我的代码:

UIButton *btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
btnSearch.frame = CGRectMake(0, 0, 22, 22);
[btnSearch setImage:[UIImage imageNamed:@"search_btn.png"] forState:UIControlStateNormal];
[btnSearch addTarget:self action:@selector(showSearch:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:_btnSearch];

self.navigationItem.rightBarButtonItems = searchItem ;

This is how it's look.
enter image description here

我想在单击搜索按钮后显示搜索栏,并在单击取消后关闭它,然后返回导航栏,但我不知道如何编码。

- (IBAction)showSearch:(id)sender{
    ???
}

This what I want. enter image description here

请帮忙或提供一些示例代码。我真的很需要它。

感谢您的阅读。


添加属性UISearchBar *mySearchBar到你的 viewController 作为

@property(nonatomic, retain) UISearchBar *mySearchBar;

符合UISearchBarDelegate as

@interface HomeViewController () <UISearchBarDelegate>
...
...
@end

然后实施showSearch方法如

-(void)showSearch:(id)sender {
if(!mySearchBar) {
    mySearchBar = [[UISearchBar alloc] init];
    [mySearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
    [mySearchBar setShowsCancelButton:YES animated:YES];
    [self.view addSubview: mySearchBar];
    mySearchBar.delegate = self;
    self.navigationController.navigationBarHidden = YES;
}else {
    searchBar.alpha = 1.0;
    self.navigationController.navigationBarHidden = YES;
}

然后将搜索栏委托方法实现为:

 - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
        self.navigationController.navigationBarHidden = NO;
        [mySearchBar setAlpha:0.0];
    }

希望你通过这样做有了这个想法。如果您愿意,您也可以将其直接添加到导航控制器本身,然后使用显示/隐藏searchBar alone.

您可以将其添加到导航控制器,就像初始化一样mysearchBar并将其添加到navigationBar as :

   UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:mySearchBar];
   self.navigationItem.rightBarButtonItem = searchBarItem;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何显示和隐藏 UISearchDisplayController 的 UISearchBar 的相关文章

  • NSTextField:当用户单击文本字段之外时结束编辑

    我有一个NSTextField我根据用户操作设置可编辑 我想在用户单击窗口内文本字段之外的任何位置时结束编辑 看起来很简单 但我无法让它发挥作用 我实施了controlTextDidEndEditing and textDidEndEdit
  • 如何在 UICollectionView 上方添加搜索栏?

    我想允许我的应用程序的用户使用UISearchBar上面一个UICollectionView 根据我的理解 一个UICollectionView必须在一个UICollectionViewController才能正常工作 但是 Xcode 不
  • 使用线程安全单例初始化代码时代码执行停止

    为了利用全局变量和方法 我实现了 Singleton 作为一种健康的编码实践 我跟着苹果文档 http www johnwordsworth com 2010 04 iphone code snippet the singleton pat
  • 不使用 MFMailComposeViewController 发送邮件

    我想从 iPhone 应用程序发送邮件而不显示MFMailComposeViewController 我还希望从用户的默认邮件帐户发送此邮件 是否有可能做到这一点 iPhone SDK 不支持这一点 可能是因为 Apple 不希望您这样做
  • 使用 UIImagePickerController 选择图像后,照片库视图保留在屏幕上

    当我通过 UIImagePickerController 界面从照片库中选择一张图片后 照片库视图保持显示 即使我调用了解雇模型视图控制器动画 in imagePickerController didFinishPickingImage e
  • iOS - 单元测试异步代码

    我试图测试的方法部分如下 void configureTableFooterView dispatch async dispatch get main queue self tableView tableFooterView nil if
  • 调整在drawRect中绘制的矩形的大小

    我有两个UIViews 我的目标是绘制包含这两个的最小矩形UIViews 我想用我要摆脱的框架画一个矩形 CGRectUnion view1 frame view2 frame 但是当我移动两者中的任何一个时UIViews 我需要更新轮廓矩
  • 没有这样的模块“Ensembles”错误 - 导入 Objective-C 框架以在 swift 项目中使用

    我将 Ensembles 添加到我的 Swift 项目 在这里找到https github com drewmccormack ensembles https github com drewmccormack ensembles 我没能在我
  • 如何在导航栏中添加右键?

    我有一个问题要在导航栏中添加右键 我有两个视图 视图 A 和视图 B 我添加了一个导航栏来查看A 之后我使用了self navigationController pushViewController显示视图 B 视图B的导航栏左侧自动显示一
  • UISegmentedControl 中的自定义字体禁用调整FontSizeToFitWidth

    我已经为我的 UISegmentedControl 设置了自定义字体 但它似乎禁用了默认字体自动调整字体大小以适合宽度范围 Before After 这是我用来设置自定义字体的代码 UISegmentedControl segmentedC
  • 释放 Core Foundation 对象引用

    我是否需要释放 Core Foundation 对象来清理内存 如果是这样 怎么办 例如 在代码中 ABAddressBookRef addressBook ABAddressBookCreate CFArrayRef peopleArra
  • NSDictionary 上的 NSPredicate

    我试图根据字母表在表格视图中创建部分 并在这些部分下按字母顺序对我的条目进行排序 我已经收集了 bandArrayIndex 中 bandArray 每个条目的第一个字母 现在我尝试使用 NSPredicate 来计算每个字母有多少个 我正
  • 在 uilabel 中查找文本的位置 {x,y}

    我有一个来自服务器的字符串 我正在 UILabel multiligne 上显示它 在该字符串中 我正在识别一些特定的子字符串 我想在该子字符串上放置一个按钮 按钮将是 UILabel 的子视图 为此 我需要子字符串坐标 我经历过这个 但我
  • 您是否标记 UIView 或将它们保留为属性?

    这主要是一个风格问题 但自从我开始为 iPhone 编程以来 我一直很好奇其他人的想法是什么 当您的 iPhone 应用程序中有一个 UIView 并且需要在应用程序的其他位置访问它时 通常在视图控制器中的另一个函数中 您是否喜欢用整数标记
  • 最小的 iOS 蓝牙管理器示例

    我一直在构建一个最小的示例 用于使用 iOS 5 0 中的 BluetoothManager 私有框架来检测附近的蓝牙设备 使用此问题中找到的答案 寻找触手可及的通用蓝牙设备 https stackoverflow com question
  • 如何在 NSMutableArray 中实现“按值分组”?

    我正在使用 NSMutableArray 我想像在 SQL 中那样按日期获取值group by log date logMuArray log currenttime 4 30pm log date 11 12 2011 log durat
  • NSString – 静态还是内联?有性能提升吗?

    如果我写的话会有任何性能提升吗 NSString helloStringWithName NSString name static NSString formatString Hello return NSString stringWith
  • 在横向中自动调整 UITableCells 内容的大小

    在 UITableView 中 我通过 UILabels 将内容添加到单元格中 定义最佳尺寸 与单元格宽度允许的一样大 我注意到只有tableView contentSize width是可靠的 因为cell contentView bou
  • 在 Objective C 的类方法中引用类本身

    我希望我没有跳过 ObjC 手册中的这一部分 但是是否可以从类的一个类方法中引用该类 就像在 PHP 中一样 您将使用 this 来引用当前实例 而 self 引用实例的类 this 的 ObjC 等价物将是 self 那么 PHP 的 s
  • UIPanGestureRecognizer 对坐标的限制

    我在主 UIView 中添加了一个子视图 称为panel 并且我向其中添加了gestureRecognizer 因为我希望它只能在Y轴上拖动并且只能在某些限制下 即160 300 超过300它不能拖动 我以这种方式实现了手势处理 IBAct

随机推荐