import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/**
let sc = UIScrollView(frame:CGRect(x: 0,y: 100,width: UIScreen.mainScreen().bounds.width,height: 44))
sc.backgroundColor = UIColor.redColor()
view.addSubview(sc)
let width = 80
let count = 15
for i in 0..<count {
let btn = UIButton()
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("标题\(i)", forState: UIControlState.Normal)
btn.frame = CGRect(x: i*width,y: 0,width: width,height: 44)
sc.addSubview(btn)
}
sc.contentSize = CGSize(width: count*width,height: 44)
**/
let sc = creatScrollView({ () -> Int in
return 5
}) { (index) -> UIView in
let width = 80
let btn = UIButton()
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("标题\(index)", forState: UIControlState.Normal)
btn.frame = CGRect(x: index*width,y: 0,width: width,height: 44)
return btn
}
view.addSubview(sc)
}
func creatScrollView(btnCount:() -> Int,btnWithIndex:(index:Int) -> UIView) ->UIScrollView
{
let sc = UIScrollView(frame:CGRect(x: 0,y: 100,width: UIScreen.mainScreen().bounds.width,height: 44))
sc.backgroundColor = UIColor.redColor()
view.addSubview(sc)
let count = btnCount()
for i in 0..<count {
let subView = btnWithIndex(index: i)
sc.addSubview(subView)
sc.contentSize = CGSize(width: CGFloat(count) * subView.bounds.width,height: 44)
}
return sc
}
}