【问题标题】:UISearchController searchBar in tableHeaderView animating out of the screentableHeaderView 中的 UISearchController searchBar 动画出屏幕
【发布时间】:2014-12-01 02:13:50
【问题描述】:

我有一个UISearchController 和一个UITableViewController 作为searchResultsController,这个searchControllerUISearchBar 设置在我的根ViewController 中显示的当前tableViewtableHeaderView 中。正如预期的那样,几乎一切都运行良好。但是在UISearchBar 的动画中(当我点击searchBar 并且UINavigationBar 隐藏并且searchBar 转到顶部,如UISearchDisplayController 中)我有一个奇怪的行为。它没有移动到UINavigationBar (y: 0) 的位置,而是跳出屏幕,然后开始显示取消按钮的动画。我尝试将我的实例化代码移动到viewDidLoad 而不是init,但事情是一样的。我认为问题的中心在于searchResultsController 的视图框架,但我不确定(我尝试设置框架,但没有成功)。我所做的一切都是纯代码。

以下是代码的相关部分:

- (void) viewDidLoad { 
    [super viewDidLoad];

    // search controller setup
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;

    [self.searchController.searchBar sizeToFit];
    self.tableView.tableHeaderView = self.searchController.searchBar;

    self.searchController.definesPresentationContext = YES;
}

我对@9​​87654337@ 有一个延迟加载:

- (UITableViewController *)searchResultsController {
    if (_searchResultsController == nil) {
        _searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        _searchResultsController.tableView.delegate = self;
        _searchResultsController.tableView.dataSource = self;
    }
    return _searchResultsController;
}

我已经从苹果下载了示例代码,但他们使用 storyBoards 和 UITableViewCell 的 xib,SearchController 在项目中完美运行。有没有人有同样的问题?我怎样才能解决这个问题?任何解决方案或建议将不胜感激。

感谢您的关注。

【问题讨论】:

  • 你有没有发现这个问题?目前我的搜索栏也有类似的问题。
  • 不,洛伦佐。不幸的是,我为这个问题使用了已弃用的 api。使用情节提要似乎可以很好地工作(我尝试了苹果的示例应用程序),但在代码中我遇到了这个奇怪的错误。
  • 好的,谢谢。您能否详细说明您使用的有效方法?我也一直在查看 Apple 的示例,他们使用故事板作为表格和详细信息视图,但所有搜索栏组件都是在代码中完成的,这是你做的吗?
  • 没关系!我刚刚注意到我错过了 self.definesPresentationContext = YES; 这条线,它最终变得至关重要。
  • 看看这个之前的帖子是否有帮助:stackoverflow.com/questions/28326269/…

标签: ios objective-c uitableview uisearchbar uisearchcontroller


【解决方案1】:

添加

self.extendedLayoutIncludesOpaqueBars = YES;

关于viewDidLoad方法

【讨论】:

  • 将 UITableViewController 和 UISearchController 一起使用,这为我解决了这个问题。
【解决方案2】:

您是否尝试将 hidesNavigationBarDuringPresentation 设置为 false? 解决了我的头痛..

self.searchController.hidesNavigationBarDuringPresentation = false;

在我看来,将搜索栏放在导航栏中可以提供更可靠的用户体验(对于 iphone)

self.navigationItem.titleView = self.searchController.searchBar;

【讨论】:

  • 这正是我想要的,非常感谢。事实上,搜索栏应该在导航栏中。
  • 这对我也有用,谢谢!显而易见的问题是为什么?
  • 也为我工作!。谢谢。苹果的例子甚至没有提到这个选项。
  • 我有一个UIView 作为搜索栏的容器,位于导航栏和结果表视图之间。我必须将 self.definesPresentationContext 设置为 false 以防止在激活搜索栏时容器向下跳约 44 像素。
  • self.searchController.hidesNavigationBarDuringPresentation = false;也为我修好了!谢谢!
【解决方案3】:

为了让这一点更清楚,@Lorenzo 的回答对我有用。

self.definesPresentationContext = YES;

【讨论】:

  • 你是我今天的英雄!
  • 相反的对我有用!我有一个UIView 作为搜索栏的容器,位于导航栏和结果表视图之间。我必须将 self.definesPresentationContext 设置为 false 以防止在激活搜索栏时容器向下跳约 44 像素。
【解决方案4】:

试试这个:

首先你需要委托

UISearchControllerDelegate

对于斯威夫特

func willPresentSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = true
}

func willDismissSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = false
}

【讨论】:

    【解决方案5】:

    在 Swift 中,尝试:

    override func viewDidLoad() {
        edgesForExtendedLayout = []
        searchController.hidesNavigationBarDuringPresentation = false
    
        // ...
    }
    

    【讨论】:

      【解决方案6】:

      我注意到 UISearchController 在我的一个视图中完美运行,但在另一个视图中却不行。问题出在 UITableViewController 而不是 UIViewController。如果您切换到内部带有 UITableView 并适当约束的 UIViewController ,则没有问题。我用 XIB 实现了我的,它运行良好。

      【讨论】:

      【解决方案7】:

      SWIFT 3.01

      func willPresentSearchController(searchController: UISearchController){
      self.navigationController?.navigationBar.isTranslucent = true
      }
      
      func willDismissSearchController(searchController: UISearchController) {
      self.navigationController?.navigationBar.isTranslucent = false
      }
      

      【讨论】:

        【解决方案8】:

        在我的例子中,searchBar 在 tableHeaderView 中并且屏幕上没有 NavigationBar。但是 SearchBar 在变为活动状态时仍会向上重叠状态栏。防止这种情况的解决方案是设置:

        searchController.hidesNavigationBarDuringPresentation = false
        

        这很奇怪,因为正如我所说的视图控制器没有使用导航栏。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-05
          • 1970-01-01
          • 2015-02-11
          • 2020-06-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多