ios - 第一个单元格顶部的 tableHeaderView

标签 ios objective-c uitableview autolayout masonry-ios-osx

我正在尝试以编程方式创建一个在 tableHeaderView 中具有搜索栏的 tableview。由于某种原因,搜索栏出现在第一个单元格的顶部。

我正在使用 Masonry 来构建约束。

谁能指出我做错了什么。

- (void)setupViews {

...
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = self.searchBar;
...
}

- (void)updateViewConstraints {

    [self.searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(@(44));
    }];

    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.self.top.equalTo(self.view);
        make.self.bottom.equalTo(self.toolbar.mas_top);
        make.width.equalTo(self.view);
    }];
...
}

Problem

您可以在此处看到标题与单元格处于同一级别。 View hierarchy

最佳答案

感谢您的帮助,我在 GitHub 上找到了一个要点,其中谈到使用 AutoLayout 更改 tableViewHeader 的大小:

https://gist.github.com/andreacremaschi/833829c80367d751cb83

- (void) sizeHeaderToFit {
    UIView *headerView = self.tableHeaderView;

    [headerView setNeedsLayout];
    [headerView layoutIfNeeded];
    CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    headerView.frame = ({
        CGRect headerFrame = headerView.frame;
        headerFrame.size.height = height;
        headerFrame;
    });

    self.tableHeaderView = headerView;
}

如果我在 updateViewConstraints 期间调用此方法,它就会起作用。

但是,我并不完全理解。

关于ios - 第一个单元格顶部的 tableHeaderView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28782319/

相关文章:

ios - 删除(清除)iOS 应用程序所有钥匙串(keychain)数据的正确方法

ios - 根据物体的速度和旋转找到所需的点

iphone - 如何自定义tableView剖面 View - iPhone

ios - 在 TableView 中数据被覆盖

iphone - iOS 视网膜显示 : images double size

ios - 更改 collectionView 中的图像以匹配数组中保存的购买项目

iPhone ABPeoplePickerNavigationController - 如何从地址簿中选择一个人的两个不同多值属性的两个单个条目

ios - 跑卫登录

iOS - UITableViewCell dealloc 错误中的自定义 UISwitch

iphone - 如何将单元格添加到tableview