SnapKit swift版的自动布局框架,第一次使用感觉还不错。

SnapKit是一个优秀的第三方自适应布局库,它可以让iOS、OS X应用更简单地实现自动布局(Auto Layout)。
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实现高度自适应的新浪微博布局的更多相关文章

  1. ios swift UITextView高度自适应

    在ios开发中,用到多行输入时一般都会用到UITextView.常见的比如说聊天输入框,评论输入框等,当用户输入多内容时,我们希望高度能根据用户输入的内容扩大而扩大.其实实现这个功能也不是很难,只需要 ...

  2. [Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. css经典布局——头尾固定高度中间高度自适应布局

    转载:穆乙 http://www.cnblogs.com/pigtail/ 相信做过后台管理界面的同学,都非常清楚这个布局.最直观的方式是框架这个我不想多写费话,因为我们的重心不在这里.如果有不了解的 ...

  4. 典型的DIV CSS三行二列居中高度自适应布局

    如何使整个页面内容居中,如何使高度适应内容自动伸缩.这是学习CSS布局最常见的问题.下面就给出一个实际的例子,并详细解释.(本文的经验和是蓝色理想论坛xpoint.guoshuang共同讨论得出的.) ...

  5. flex布局嵌套之高度自适应

    查遍各大资源无任何flex嵌套布局的例子,经过自己折腾完成了项目中的高度自适应需求(更多应用于前端组件) 效果图: html代码:(关键地方已经用颜色特别标识 ^_^) <!DOCTYPE ht ...

  6. 布局:高度已知,布局一个三栏布局,左栏和右栏宽度为200px,中间自适应

    需求:高度已知为200px,写出三栏布局,左栏和右栏各位200px,中间自适应,如下图所示: 方法一:float浮动布局 原理是:定义三个区块,需要注意的是中间的区块放在右边区块的下面,统一设置高度为 ...

  7. css高度已知,左右定宽,中间自适应三栏布局

    css高度已知,左右定宽,中间自适应三栏布局: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. CSS布局之-高度自适应

    何为高度自适应? 高度自适应就是高度能跟随浏览器窗口的大小改变而改变,典型的运用在一些后台界面中上面一栏高度固定用作菜单栏或导航栏,下面一栏高度自适应用于显示内容.高度自适应不像宽度自适应那样简单,在 ...

  9. 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局

    文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...

随机推荐

  1. jenkins-小知识点

    如果想停止jenkins运行 控制面板-服务-查看本地服务-选中jenkins 1.启动类型改为手动 2.改为禁止 使用的时候,每次都改一下状态

  2. Javascript-- jQuery 核心

    jQuery中each方法的应用 jQuery中有个很重要的核心方法each,大部分jQuery方法在内部都会调用each,其主要的原因的就是jQuery的实例是一个元素合集 如下:找到所有的div, ...

  3. inux命令学习笔记(5):rm 命令

    学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令. rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目 录及其下的所 ...

  4. 【LeetCode】036. Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. Netty,Netty

    Windows防火墙会自动关闭空闲的TCP链接,所以Netty需要心跳,如果发现链接断开需要进行关闭Session: 怎么来理解TCP的流式传输呢? int blocksize = buffer.re ...

  6. DSP/BIOS程序启动顺序

    基于TI的DSP芯片的应用程序分为两种:一般应用程序:DSP/BIOS应用程序. 为简化编程,TI提供了一套C的编程接口,它以API和宏的形式封装了TI的所有硬件模块,这套接口统称DSP/BIOS.D ...

  7. Ruby中的include

    Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ...

  8. 排序----demo----

    排序1---冒泡法: 单向冒泡排序的基本原理就是:对于给定的n个数据,从第一个数据开始一次对相邻的两个数据进行比较,当前面的数据大于后面的数据时,交换位置,进行一轮比较和换位后,n个数据中最大的那个被 ...

  9. (cdh)hive 基础知识 名词详解及架构

    过程 启动 hive 之后出现的 CLI 是查询任务的入口,CLI 提交任务给 Driver Driver 接收到任务后调用 Compiler,Executor,Optimizer 将 SQL 语句转 ...

  10. Web Form要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。”的解决办法。

    1.先将aspnet.scriptmanager.jquery.dl 复制到bin  (网站根目录下的bin文件夹找不到,看看下面的图片中点击[显示所有文档])  文件夹下.   2.在网站根目录下s ...