UIBannerView
安装
UIBannerView支持swift 5.0,iOS 12
- 添加pod 'UIBannerView’
- 通过运行pod install安装pods。
- 在您想要使用UIBannerView的地方的.swift文件中添加import UIBannerView。
基本用法
默认用法
@IBOutlet weak var bannerView: UIBannerView!
self.bannerView.setDefaultBanners(banners: `yourArray`); // object of array has to conform IBannerModel protocol
自定义单元
创建您的自定义单元。
private var yourArray = [YourArray]();
private func setUp()
{
self.bannerView.dataSource = self;
self.bannerView.delegate = self;
self.bannerView.addRegisterCustomCell(nibName: "CustomCell", forCellWithReuseIdentifier: "CustomCell");
self.bannerView.reloadData();
}
extension ViewController: UIBannerViewDataSource
{
func bannerView(_ bannerView: UIBannerView, didSelectItemAt indexPath: IndexPath)
{
print("tapped \(indexPath)");
}
}
extension ViewController: UIBannerViewDelegate
{
func bannerView(_ bannerView: UIBannerView, numberOfItemsInSection section: Int) -> Int
{
return self.customBanner.count;
}
func bannerView(_ bannerView: UIBannerView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = bannerView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell;
cell.bind(model: self.customBanner[indexPath.row]);
return cell;
}
}
注意事项
单元格宽度等于视口宽度减去填充。
项目间距是单元格和下一个单元格之间的间隔。
显示页面控制:如果关闭,则不会显示;如果开启,则显示(页面控制器)
左右边距应总共为100;如果边距超过50,则会显示过多的内容。
如果启用分页功能,则将视口视为页面;如果禁用,则显示为自由滚动。
跟踪颜色与页面控制器显示相关,将在页面滚动时标记活动页面。
依赖关系
UIBannerView 使用 SDWebImage