最近做公司项目,使用到到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. Cracking the coding interview--Q1.3

    原文 Given two strings, write a method to decide if one is a permutation of the other. 译文 给你两个字符串,写一个方 ...

  2. sicily 无路可逃?(图的DFS)

    题意:在矩阵数组中搜索两点是否可达 解法:DFS #include<iostream> #include<memory.h> using namespace std; stru ...

  3. Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)

    题目列表: 2146 Problem A [手速]阔绰的Dim 2147 Problem B [手速]颓废的Dim 2148 Problem C [手速]我的滑板鞋 2149 Problem D [手 ...

  4. 查看MySQL数据库的默认编码

    查看MySQL数据库的默认编码 1.使用status命令能够显示数据库的相关系信息,示例如下: mysql> status;————–mysql Ver 14.12 Distrib 5.0.77 ...

  5. Tomcat类加载器

    1JVM类加载机制   JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使 ...

  6. chomp方法

    chomp方法属于String类里面的: "hello".chomp #=> "hello" "hello\n".chomp #=&g ...

  7. html中的banner自适应屏幕代码

    <html> <head> <title>Title</title> <style> .bannerbox { width:100%; po ...

  8. JS(五)

    感觉JS里面还是有很多小技巧的,知道套路了,其实实现起来其实也还没有想象中的那么复杂.不过我觉得还是要把所学的知识融会贯通吧,不能学了JS就忘了前面的知识,结合起来才会威力无穷. 1.跑马灯:弹弹弹 ...

  9. 谈谈Ext JS的组件——布局的用法

    概述 在Ext JS中.包括两类布局:组件类布局和容器类布局.由于有些组件是有不同的组件组合而成的,如字段就由标题和输入框构成,他们之间也是存在布局关系的,而这就须要组件类布局来处理组件内自己特有的布 ...

  10. 查询rman备份信息经常使用指令

    查询rman备份信息经常使用指令 ----登陆到rman $rman target / ----以精简的格式查看备份信息 RMAN> list backup of database summar ...