UISearchController中的searchBar在tableHeaderView中动画出屏幕

Tia*_*aia 30 objective-c uitableview uisearchbar ios uisearchcontroller

我有一个UISearchControllerUITableViewController作为searchResultsController,在UISearchBar这个searchController设置是在tableHeaderView我目前的tableView在我的根视图控制器显示.正如预期的那样,几乎所有东西都运作良好.但是在动画中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;
}
Run Code Online (Sandbox Code Playgroud)

我有一个懒惰的负载searchResultsController:

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

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

感谢您的关注.

dex*_*ell 23

self.extendedLayoutIncludesOpaqueBars = YES;
Run Code Online (Sandbox Code Playgroud)

viewDidLoad方法


小智 21

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

self.searchController.hidesNavigationBarDuringPresentation = false;
Run Code Online (Sandbox Code Playgroud)

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

self.navigationItem.titleView = self.searchController.searchBar;
Run Code Online (Sandbox Code Playgroud)


Ced*_*ick 17

为了使这个更清楚@ Lorenzo的回答对我有用.

self.definesPresentationContext = YES;
Run Code Online (Sandbox Code Playgroud)

  • 对面对我有用!我有一个`UIView`作为搜索栏的容器,在导航栏和结果表视图之间.我必须将`self.definesPresentationContext`设置为`false`以防止容器在搜索栏被激活时向下跳过大约44个像素. (3认同)

Inu*_*sha 7

试试这个:

首先你需要委托

UISearchControllerDelegate

对于斯威夫特

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

func willDismissSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = false
}
Run Code Online (Sandbox Code Playgroud)


XFr*_*ire 5

在 Swift 中,尝试:

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

    // ...
}
Run Code Online (Sandbox Code Playgroud)