TagLayout
示例
要运行示例项目,请先从仓库中克隆项目,然后从示例目录中运行pod install
。
最低要求
- iOS 9.0
- Swift 5
- Xcode 11
安装
TagLayout可以通过CocoaPods获得。要安装它,只需将以下行添加到Podfile中
pod 'TagLayout'
使用
import TagLayout
1. 使用代码
@IBOutlet weak var collectionView: UICollectionView!
var tagLayout = TagLayout()
override func viewDidLoad() {
super.viewDidLoad()
tagLayout.delegate = self
tagLayout.scrollDirection = .horizontal
tagLayout.cellSpacing = 5.0
collectionView.setCollectionViewLayout(tagLayout, animated: true)
}
2. 使用Storyboard
将CollectionView的布局设置为Custom并将TagLayout作为布局输入。确保目标也设置为TagLayout。
在你ViewController中
if let tagLayout = collectionView.collectionViewLayout as? TagLayout {
tagLayout.delegate = self
tagLayout.scrollDirection = .horizontal
tagLayout.cellSpacing = 5.0
}
委托
extension ViewController: TagLayoutDelegate {
//Return width of the cell here.
func widthForItem(at indexPath: IndexPath) -> CGFloat {
return 100.0
}
//Return height of the cell or row here. Will remain same for all the cells
func rowHeight() -> CGFloat {
return 40.0
}
}
属性
-
cellSpacing - 单元格间的间距。默认为10.0
tagLayout.cellSpacing = 20.0
-
scrollDirection - 可以为水平或垂直。默认为垂直。
tagLayout.scrollDirection = .horizontal
方法
- reset() - 用于重置布局。使当前布局无效并从头开始重建布局。在运行时更改滚动方向或单元格大小发生变化时使用。
尝试在CollectionView上执行操作后,单元格UI显示不正常时,在布局上执行reset()。
tagLayout.reset()
贡献
我渴望看到你通过提出新特性、添加功能、报告漏洞或传播信息来为此项目作出贡献。任何帮助或贡献都将受到欢迎。
作者
udbhateja ([email protected]) bit.ly/udbhateja
许可
TagLayout 可在 MIT 许可下使用。更多信息请参阅 LICENSE 文件。