最近做公司项目,使用到到tableHeadView,一直习惯用masonry来设置约束,但是设置tableHeadView没有那么的简单。先看下效果图:

  

  

视图层次结构是这样的:

  

  基础的创建工程项目之类的就直接跳过,直接来分析:

  顶部的tableHeadView 是一个自定义的view,然后内部存在两个view 。我们设置如下:

    1> 创建一个ContainerView 继承自 UIView;

    2> 在ContainerView 添加子控件,设置Container 的底部约束

    3> 在设置tableView 的控制器处理

      •   tableView的基本设置:创建、添加到父视图、设置约束、数据源、代理等
      • 创建一个headView,来容纳带有约束的 ContainerView
      • 利用 systemLayoutSizeFittingSize:UILayoutFittingCompressedSize 计算出约束后的高度,然后设置给headView,
      • 设置tableHeadView = headView

原因:

  tableHeadView是一个自适应的视图,本身就会适应,一般的方法,你如果利用frame 来设置,没有问题,

  如果你用masonry / AutoLayout 来设置的话,自定义顶部的视图会出现各种奇葩的问题(网上有人说这事tableView 的一个bug,你们可以尝试一下)。

  如果你自定义视图,那么建议你使用外部 用一个View 来包住,根据内部的约束,来反推外部frame的高度,然后设置给headView

具体代码实现如下:

//
//  ContainerView.m
//  Masonry的内部视图视图
//
//  Created by admin on 16/1/20.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ContainerView.h"
#import "Masonry.h"

@interface ContainerView () {
    UITextView *_textView;
    UIButton *_selecteBtn;
}

@end

@implementation ContainerView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _textView = [[UITextView alloc] init];
    _selecteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    [self addSubview:_textView];
    [self addSubview:_selecteBtn];

    _textView.backgroundColor = [UIColor blueColor];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self);
        make.height.equalTo(@);
    }];

    _selecteBtn.backgroundColor = [UIColor purpleColor];
    [_selecteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).offset();
        make.left.right.equalTo(self);
        make.height.equalTo(@);
    }];

    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_selecteBtn);
    }];
}

@end
//
//  ViewController.m
//  Masonry的内部视图视图
//
//  Created by admin on 16/1/20.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewController.h"
#import "ContainerView.h"
#import "Masonry.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    UITableView *tableView =[[UITableView alloc]init];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
    tableView.dataSource =self;
    tableView.delegate =self;
    [self.view addSubview:tableView];

    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset();
    }];

    [tableView setNeedsLayout];
    [tableView layoutIfNeeded];

    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(, , ,)];

    ContainerView *con = [[ContainerView alloc] init];
    con.backgroundColor = [UIColor blackColor];
    [headView addSubview:con];

    [con mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(headView);
    }];

    CGFloat height = [headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headView.frame;
    frame.size.height = height;

    headView.frame = frame;

    tableView.tableHeaderView = headView;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    return cell;
}

@end

iOS开发 masonry 设置tableHeadView的更多相关文章

  1. iOS开发--应用设置及用户默认设置——转载

    [链接]iOS开发--应用设置及用户默认设置[1.bundlehttp://www.jianshu.com/p/6f2913f6b218 在iphone里面,应用都会在“设置”里面有个专属的应用设置, ...

  2. iOS开发-Masonry简易教程

    关于iOS布局自动iPhone6之后就是AutoLayOut,AutoLayOut固然非常好用,不过有时候我们需要在页面手动进行页面布局,VFL算是一种选择,如果对VFL不是很熟悉可以参考iOS开发- ...

  3. iOS开发中设置UITextField的占位文字的颜色,和光标的颜色

    在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...

  4. iOS开发--应用设置及用户默认设置【2、读取应用中的设置】

            在上一节中,我们通过探讨应用的系统设置的基本功能,了解运用bundle捆绑包以及plist文件的基本开发.用户能够使用设置应用来声明他们的偏好设置,那么我们怎样去调用用户所设置的参数呢 ...

  5. iOS开发--应用设置及用户默认设置【1、bundle的运用】

           在iphone里面,应用都会在“设置”里面有个专属的应用设置,选择该菜单界面,用户便可以在其中输入和更改各种选项,协助用户更便捷设置个人喜好与习惯. 在这一节中,希望能通过对捆绑包(bu ...

  6. IOS开发中设置导航栏主题

    /** * 系统在第一次使用这个类的时候调用(1个类只会调用一次) */ + (void)initialize { // 设置导航栏主题 UINavigationBar *navBar = [UINa ...

  7. IOS开发中设置控件内容对齐方式时容易混淆的几个属性

    IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NST ...

  8. iOS开发 Masonry的简单使用

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...

  9. iOS 开发常用设置

    1. iOS设置app应用程序文件共享 设置流程 xcode 打开项目----在 info.plist 文件,添加 UIFileSharingEnabled 并设置属性为 YES, 在app内部,将您 ...

随机推荐

  1. 用Setup系列函数完成驱动卸载安装[驱动安装卸载程序]

    // InstallWDFDriver.cpp : Defines the entry point for the console application. // #include "std ...

  2. BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者

    3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 47  Solve ...

  3. 30种IT技能让你年薪过10万美元!

    科技行业的高薪岗位向来不少,但在不断变化的职场中,热门技能却随时在变:今天的热门技术,明天可能就会过时.美国求职网站 Dice.com 最近发布了 2015 年薪酬报告,通过对 23,470 位 IT ...

  4. sicily 1024 Magic Island

    题意:求无向图路径中的最大带权值. 解法:深搜 // Problem#: 9859 // Submission#: 2661875 // The source code is licensed und ...

  5. EBuild-API常见问题汇总

    问题1:用户访问被防火墙屏蔽检查处理流程 E-Build API(原IBE)是面向航空公司和代理人,以及第三方的航空预订服务产品.用户通过客户端,使用固定IP访问E-Build API服务器,航信网络 ...

  6. 知方可补不足~用xsl来修饰xml

    概念相关 XSL是可扩展样式表语言的外语缩写,是一种用于以可读格式呈现 XML(标准通用标记语言的子集)数据的语言. 起始于 XSL 万维网联盟(W3C)开始发展 XSL 的原因是:存在着对于基于 X ...

  7. SQL Server error "Xml data type is not supported in distributed queries" and workaround for it

    Recently while working with data migration,got an error while running a following query where Server ...

  8. Java基础知识强化49:10个实用的但偏执的Java编程技术

    1. 将String字符串放在最前面 为了防止偶发性的NullPointerException 异常,我们通常将String放置在equals()函数的左边来实现字符串比较,如下代码: // Bad ...

  9. linux下CDROM挂载

    在VM-->removableDevice-->CD DVD-->加载iso镜像文件: [root@rusky2 mnt]# mount /dev/cdrom /mnt/cdrom ...

  10. Drawable与Bitmap 自定义

    Drawable简介 Drawable是Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF.PNG.JPG,当然也支持BMP.相比于View,我们并不需要去考虑如何measure. ...