UISrollView,使用约束,不使用Frame,
这不能够使用contentView,必须手动添加容器视图。
import UIKit
class CBRecommendADCell: UITableViewCell {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageCtrl: UIPageControl!
//数据
var bannerArray: Array<CBRecommendBannerModel>? {
didSet {
//显示UI
showData()
}
}
func showData(){
let cnt = bannerArray?.count
if cnt > 0 {
//0.添加一个容器视图
let containerView = UIView.createView()
scrollView.addSubview(containerView)
//设置约束
containerView.snp_makeConstraints(closure: {
[weak self]
(make) in
make.edges.equalTo(self!.scrollView)
make.height.equalTo(self!.scrollView)
})
var lastView: UIView? = nil
for i in 0..<cnt! {
//1.获取模型对象
let model = bannerArray![i]
//2.创建图片
let tmpImageView = UIImageView.createImageView(nil)
//在线加载图片
/*
第一个参数:图片网址的url
第二个参数:默认图片
第三个参数:选项
第四个参数:可以获取下载的进度
第五个参数:下载结束的时候的操作
*/
let url = NSURL(string: model.banner_picture!)
let image = UIImage(named: "sdefaultImage")
tmpImageView.kf_setImageWithURL(url, placeholderImage: image, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
containerView.addSubview(tmpImageView)
//约束
tmpImageView.snp_makeConstraints(closure: { (make) in
make.top.bottom.equalTo(containerView)
make.width.equalTo(kScreenWidth)
if i == 0 {
make.left.equalTo(containerView)
}else{
make.left.equalTo((lastView?.snp_right)!)
}
})
lastView = tmpImageView
}
//修改容器视图的约束
containerView.snp_makeConstraints(closure: { (make) in
make.right.equalTo((lastView?.snp_right)!)
})
//修改分页控件
pageCtrl.numberOfPages = cnt!
//设置代理
scrollView.delegate = self
scrollView.pagingEnabled = true
}
}