UITearchBar UITableViewHeader的子视图? [英] UISearchBar subview of UITableViewHeader?

查看:62
本文介绍了UITearchBar UITableViewHeader的子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将UISearchBar添加到已经具有标题视图的UITableView。当我尝试将搜索栏添加到现有的标题视图时,它一直有效,直到我点击它,此时我得到视图层次结构没有为约束做好准备,似乎是因为搜索栏不是tableview的直接子视图,所以当UISearchController尝试更新它不能的约束时。

I want to add a UISearchBar to a UITableView that already has a header view. When I try and add the search bar to the existing header view it works until I tap on it, at which point I get The view hierarchy is not prepared for the constraint, which appears to be because the search bar is not a direct subview of the tableview so when the UISearchController tries to update the constraints it can't.

唯一的方法是我发现将表视图标题作为搜索栏,然后一切正常,但当然我丢失了标题视图中已有的其他视图。

The only way around this that I've found is making the table view header the search bar, then everything works fine, but of course then I lose the other views that were already in the header view.

推荐答案

为了解决这个问题,我将搜索栏放在容器 UIView 中。将约束应用于此容器视图,并对容器内的搜索栏使用自动调整掩码。

To get around this behavior, I put my search bar in a container UIView. Apply the constraints to this container view and use an autoresizing mask for the search bar within the container.

// Configure header view
UIView *headerView = ...
...

// Create container view for search bar
UIView *searchBarContainer = [UIView new];
searchBarContainer.translatesAutoresizingMaskIntoConstraints = NO;
[searchBarContainer addSubview:self.searchBar];
[headerView addSubview:searchBarContainer];
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Apply constraints involving searchBarContainer
[headerView addConstraint: ...];
...

// Then add header to table view
self.tableView.tableHeaderView = headerView;

这篇关于UITearchBar UITableViewHeader的子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆