【问题标题】:How to add UISearchBar to UIView?如何将 UISearchBar 添加到 UIView?
【发布时间】:2014-05-16 22:44:59
【问题描述】:

我想将UISearchBar 添加到UIView 而不是UIViewController,问题是UISearchDisplayController 的init 方法需要UIViewController 作为contentsController:。 我可以看到UISearchBar 正确显示在视图中,但是如果我单击它,该栏就会消失。这是我目前正在使用的代码:

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    // this was my approach, but this DOES NOT work
    UIViewController *results = [[UIViewController alloc] init];
    results.view.userInteractionEnabled = NO;
    [self insertSubview:results.view aboveSubview:self.menuList];

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:results];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.tableView.tableHeaderView = self.searchBar;

之前:

点击进入搜索栏后:

【问题讨论】:

  • 能否请您添加屏幕截图
  • @James03 我已经编辑了我的答案。
  • 尝试在viewDidLoad:中添加这个self.edgesForExtendedLayout = UIRectEdgeNone;
  • 问题是,搜索栏在自定义 UIView 中而不是在 UIViewController 中。我很确定这会导致问题,但不幸的是我没有其他可能性,因为我的 UIView 是一个下拉菜单。

标签: ios ios7 uisearchbar uisearchdisplaycontroller


【解决方案1】:

很快,

我使用hidesNavigationBarDuringPresentation来解决这个问题。

这个问题好像是点击搜索栏后会自动隐藏搜索栏。在视图之外单击就可以了。

我添加了这一行:

self.searchController?.hidesNavigationBarDuringPresentation = false;

希望有效果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 2023-03-21
    • 2019-05-09
    • 2021-05-07
    • 2023-04-05
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多