SnapKit swift实现高度自适应的新浪微博布局
SnapKit swift版的自动布局框架,第一次使用感觉还不错。
GtiHub地址:https://github.com/SnapKit/SnapKit
1.uitableview高度自适应的关键代码:
self.tableView.estimatedRowHeight=50//预估值随意写一个差不多的就可以
self.tableView.rowHeight=UITableViewAutomaticDimension
2.布局约束时一定要从上往下不然高度也无法适应
3.在最后一个view一定要添加与底部的一个约束: make.bottom.equalTo(-10)
以下是示例代码
//
// TableViewCell.swift
// Test
// Copyright © 2017年 All rights reserved.
//
import UIKit
import SnapKit
class TableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
//约束头像
self.headImageView.backgroundColor=UIColor.red
self.addSubview(headImageView)
self.headImageView.snp.makeConstraints({make in
make.top.left.equalTo(10)
make.width.height.equalTo(40)
})
//约束用户名
self.addSubview(nameLabel)
self.nameLabel.snp.makeConstraints({make in
make.top.equalTo(headImageView.snp.top)
make.left.equalTo(headImageView.snp.right).offset(5)
})
self.addSubview(timeLabel)
self.timeLabel.snp.makeConstraints({make in
make.left.equalTo(headImageView.snp.right).offset(5)
make.bottom.equalTo(headImageView.snp.bottom)
})
//约束文字内容
self.addSubview(contentLabel)
self.contentLabel.snp.makeConstraints({make in
make.top.equalTo(headImageView.snp.bottom).offset(5)
make.left.equalTo(headImageView.snp.left)
make.right.equalTo(-10)
})
//约束图片
self.picView.dataSource=self
self.picView.delegate=self
// let nib = UINib.init(nibName: "CollectionViewCell", bundle: nil)
self.picView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "imgCell")
self.addSubview(picView)
self.picView.snp.makeConstraints({make in
make.top.equalTo(contentLabel.snp.bottom).offset(5)
make.height.equalTo(0)
make.left.equalTo(contentLabel.snp.left)
make.right.equalTo(-10)
})
//约束底部工具条
self.addSubview(bottomBar)
self.bottomBar.snp.makeConstraints({make in
make.top.equalTo(picView.snp.bottom).offset(5)
make.left.equalTo(10)
make.right.equalTo(-10)
make.height.equalTo(40)
make.bottom.equalTo(-10)//这句一定要放在最后一个view不然无法自动计算高度
})
}
//懒加载头像
lazy private var headImageView:UIImageView={
let headImageView = UIImageView()
return headImageView
}()
//懒加载用户名
lazy private var nameLabel:UILabel={
let nameLabel=UILabel()
return nameLabel
}()
//懒加载时间
lazy private var timeLabel:UILabel={
let timeLabel = UILabel()
timeLabel.font=UIFont.systemFont(ofSize: 14)
timeLabel.textColor=UIColor.darkGray
return timeLabel
}()
//懒加载文字内容
lazy private var contentLabel:UILabel={
let contentLabel = UILabel()
return contentLabel
}()
//懒加载图片容器
lazy private var picView:UICollectionView={
let picView=UICollectionView(frame:CGRect.zero,collectionViewLayout: UICollectionViewFlowLayout())
return picView
}()
//底部工条
lazy private var bottomBar:UIView={
let bottomBar=UIView()
bottomBar.backgroundColor=UIColor.cyan
return bottomBar
}()
var model:Model?{
didSet{
guard let model=model else {
return
}
self.nameLabel.text="用户名"
self.timeLabel.text="时间"
self.contentLabel.text=model.text ?? ""
//这里更新图片容器(注意:这里测试写死的,开发中应根据图片张数计算图片容器高度)
self.picView.snp.updateConstraints({make in
make.height.equalTo(310)
})
}
}
}
extension TableViewCell:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:(UIScreen.main.bounds.size.width-30)/3,height:100)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imgCell", for: indexPath)
cell.backgroundColor=UIColor.orange
return cell
}
}
//
// TableViewController.swift
// Test
//
// Created by admin
// Copyright © 2017年 tdin360. All rights reserved.
//
import UIKit
class TableViewController: UITableViewController {
var datas = [Model]()
override func viewDidLoad() {
super.viewDidLoad()
datas.append(Model(text: "2016.01.18 -- 推出“普通简化版”tableview的cell自动高度方法(推荐使用),原来的需2步设置的普通版方法将标记过期"))
datas.append(Model(text: "One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime."))
datas.append(Model(text: "One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime."))
datas.append(Model(text: "One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime."))
datas.append(Model(text: "One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime."))
//这两句是实现高度自适应的关键代码
self
self.tableView.rowHeight=UITableViewAutomaticDimension
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datas.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.model=datas[indexPath.row]
return cell
}
}
效果图

SnapKit swift实现高度自适应的新浪微博布局的更多相关文章
- ios swift UITextView高度自适应
在ios开发中,用到多行输入时一般都会用到UITextView.常见的比如说聊天输入框,评论输入框等,当用户输入多内容时,我们希望高度能根据用户输入的内容扩大而扩大.其实实现这个功能也不是很难,只需要 ...
- [Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- css经典布局——头尾固定高度中间高度自适应布局
转载:穆乙 http://www.cnblogs.com/pigtail/ 相信做过后台管理界面的同学,都非常清楚这个布局.最直观的方式是框架这个我不想多写费话,因为我们的重心不在这里.如果有不了解的 ...
- 典型的DIV CSS三行二列居中高度自适应布局
如何使整个页面内容居中,如何使高度适应内容自动伸缩.这是学习CSS布局最常见的问题.下面就给出一个实际的例子,并详细解释.(本文的经验和是蓝色理想论坛xpoint.guoshuang共同讨论得出的.) ...
- flex布局嵌套之高度自适应
查遍各大资源无任何flex嵌套布局的例子,经过自己折腾完成了项目中的高度自适应需求(更多应用于前端组件) 效果图: html代码:(关键地方已经用颜色特别标识 ^_^) <!DOCTYPE ht ...
- 布局:高度已知,布局一个三栏布局,左栏和右栏宽度为200px,中间自适应
需求:高度已知为200px,写出三栏布局,左栏和右栏各位200px,中间自适应,如下图所示: 方法一:float浮动布局 原理是:定义三个区块,需要注意的是中间的区块放在右边区块的下面,统一设置高度为 ...
- css高度已知,左右定宽,中间自适应三栏布局
css高度已知,左右定宽,中间自适应三栏布局: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- CSS布局之-高度自适应
何为高度自适应? 高度自适应就是高度能跟随浏览器窗口的大小改变而改变,典型的运用在一些后台界面中上面一栏高度固定用作菜单栏或导航栏,下面一栏高度自适应用于显示内容.高度自适应不像宽度自适应那样简单,在 ...
- 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局
文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...
随机推荐
- LSM Tree 学习笔记——MemTable通常用 SkipList 来实现
最近发现很多数据库都使用了 LSM Tree 的存储模型,包括 LevelDB,HBase,Google BigTable,Cassandra,InfluxDB 等.之前还没有留意这么设计的原因,最近 ...
- jQuery购物数量数字加减运算效果
<a href="###" id="add" value="+">+</a> <input type=&quo ...
- 第一章 走进Python
目标 了解Python的历史 了解Python的特征 了解Python的应用 掌握Linux下Python开发环境的搭建 理解Windows下Python环境搭建 案例 安装Python,写出第一个P ...
- 关于MFC资源句柄、ID和对象
一.资源.句柄和ID 资源: MFC中的资源,如菜单.对话框.图标.工具条.对话框等,是windows创建的,并占用堆内存.windows在创建这些资源时候会给每个资源分配一个句柄,用来标记这些资源, ...
- 关于MFC视图文档框架的理解-1
一.单个文档和多个文档的区别: MFC分为对话框程序,单文档程序和多文档程序. 单个文档程序:一个主框架内仅允许打开一个视图,若要打开另一个,则当前的文档必须得关闭.单文档程序可以打开不同格式的文档. ...
- C++中函数重载
C++中函数重载使用顶层const修饰参数和不使用const修饰参数效果是一样的,如果定义了这样的重载函数会报函数重定义的错误. 追其原因,C++中的函数传递方式有三种,一种是值传递,就是拷贝,一种是 ...
- PS 滤镜— —Marble 效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- jquery中ON方法的使用
以前在jquery中绑定动态元素一直使用live,现在才发现live已经被抛弃了,现在如果想实现live方法,可以使用最新的ON方法,具体使用如下: 替换live() live()写法 $('#l ...
- oracle隐含参数的查看与修改
v$parameter视图中查询参数的时候其实都是通过x$ksppi和x$ksppcv这两个内部视图中得到的. 1. 可以通过如下方式查询当前实例的所有隐含参数: col name for a30 ...
- Sublime Text3常用插件以及安装方法(实用)【转载】
https://www.cnblogs.com/liuchaoH/p/6370008.html Package Control组件在线安装 按Ctrl+`调出console(注:避免热键冲突) 粘贴以 ...