UITableView HeaderView,FooterView 使用SnapKit布局导致约束异常
今天做一个APP里面设置页面(个人中心) 就是一个列表菜单 顶部是一个头像和账户标题, 底部为一个退出登录按钮
当然我第一时间就想到了UITableView, HeaderView, FooterView
// 我创建了两个类文件, 用来做UITableView 的header, footer
class SettingHeaderView: UIView {
//里面的布局是采用 SnapKit 布局
}
class SettingFooterView: UIView {
}
期初我是这样写的, 但是运行就报错了
override func viewDidLoad() {
super.viewDidLoad()
//1.创建header,footerView
let headView = SettingHeaderView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 120))
let footerView = SettingFooterView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 150))
//2.赋值给UITableView
self.tableView.tableHeaderView = headView
self.tableView.tableFooterView = footerView
self.tableView.dataSource = self
self.tableView.delegate = self
self.view.addSubview(self.tableView)
self.tableView.mas_makeConstraints { (maker) in
maker?.top.equalTo()(self.mas_topLayoutGuide)
maker?.left.right()?.equalTo()(self.view)
maker?.bottom.equalTo()(self.view.mas_bottom)
}
}

我第一时间想到了, 是UITableView没进行刷新布局, 我加上了setNeedsLayout 和 layoutIfNeeded 依旧不可, 我又想到, 是否我添加的header 和footer也需呢?
但是也是错误了, 后来我想了下, 修改了代码的顺序
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.view.addSubview(self.tableView)
self.tableView.mas_makeConstraints { (maker) in
maker?.top.equalTo()(self.mas_topLayoutGuide)
maker?.left.right()?.equalTo()(self.view)
maker?.bottom.equalTo()(self.view.mas_bottom)
}
//必要的代码, 刷新TableView
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
//1.创建header,footerView
let headView = SettingHeaderView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 120))
let footerView = SettingFooterView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 150))
//2.赋值给UITableView
self.tableView.tableHeaderView = headView
self.tableView.tableFooterView = footerView
//必要的代码, 刷新TableView
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
}
UITableView HeaderView,FooterView 使用SnapKit布局导致约束异常的更多相关文章
- 去掉UITableView HeaderView或FooterView随tableView 移动的黏性
去掉UITableView HeaderView或FooterView随tableView 移动的黏性(sticky) 控制器中实现以下方法即可: - (void)scrollViewDidScrol ...
- UICollectionView添加 HeaderView FooterView
UICollectionView显示HeaderView FooterView 不如UITableView那么容易,常用会有两种做法: 1.Xib或者Storyboard 在属性一栏中设置一下: 如图 ...
- 去掉UItableview headerview黏性(sticky)
// 去掉UItableview headerview黏性(sticky) - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFlo ...
- 去掉UItableview headerview黏性
//去掉UItableview headerview黏性 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView ...
- iOS设置tableViewCell之间的间距(去掉UItableview headerview黏性)
经常在项目中遇到自定义cell的情况,而且要求cell之间有间距,但是系统没有提供改变cell间距的方法,怎么办? 方法1:自定义cell的时候加一个背景View,使其距离contentView的上下 ...
- alias导致virtualenv异常的分析和解法
title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...
- 扩展Exception,增加判断Exception是否为SQL引用约束异常方法!
在设计数据表时,如果将某些列设置为关联其它表的外键,那么如果对其进行增加.修改操作时,其关联表若没有相匹配的记录则报错,或者在对其关联表进行删除时,也会报错,这就是外键约束的作用,当然除了外键还有许多 ...
- VS2012外接程序VMDebugger未能加载或导致了异常
转http://blog.csdn.net/maryhuan/article/details/42676915 故障现象:打开Visual Studio 2010后弹出错误框,外接程序VMDebugg ...
- Fragment已经被added了导致的异常。
java.lang.IllegalStateException: Fragment already added: ******Effect 出现的原因是commit方法提交是异步的,所以容易出现,判 ...
随机推荐
- python数据挖掘第三篇-垃圾短信文本分类
数据挖掘第三篇-文本分类 文本分类总体上包括8个步骤.数据探索分析->数据抽取->文本预处理->分词->去除停用词->文本向量化表示->分类器->模型评估.重 ...
- django补充CBV和FBV模式
django补充CBV和FBV模式FBV模式---函数:经常用的方式CBV模式---类CBV中url连接时函数名后面要接.as_view()class index(views.View): @... ...
- 安全性测试:OWASP ZAP 2.8 使用指南(四):ZAP扫描移动应用安全
在做移动应用(APP,小程序等)测试时,需要关注应用安全性. ZAP是可以用来进行手机移动应用渗透性测试扫描的. 正因为ZAP是采用“中间代理”的形式,截取并扫描所有客户端与服务器的交互请求,作为客户 ...
- django nginx 504 time-out 错误
报错: 分析 nginx和uwsgi整合时有三个参数可以用于设置超时时间: 1.uwsgi_connect_timeout: 默认60秒,与uwsgi-server连接的超时时间,该值不能超过75秒. ...
- day 26-1 property、绑定与非绑定方法
property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值:就是把一个函数属性的访问方式变成像访问数据属性的方式一样. 我们首先来看一个对比效果 例一:在调用 bmi ...
- 【转载】C#string.Formart的字符串格式化
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项.Stri ...
- Dynamics 365利用Web API对视图进行查询
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- Data Guard Physical Standby - RAC Primary to RAC Standby 使用第二个网络 (Doc ID 1349977.1)
Data Guard Physical Standby - RAC Primary to RAC Standby using a second network (Doc ID 1349977.1) A ...
- Vue项目中使用jquery插件
1.引入jquery,并且在vue.config.js里配置 config.plugin('provide') .use(webpack.ProvidePlugin, [{ $: 'jquery', ...
- python 导入同级目录文件时报错
当你import的时候,python解释器只会在sys.path这个变量(一个list,你可以print出来看)里面的路径中找可能匹配的package或module. 而一个package跟一个普通文 ...