importUIKit
classViewController:UIViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
/*
let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
sc.backgroundColor = UIColor.redColor()
let count = 15
let width = 50
for i in 0..
let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.textColor = UIColor.darkGrayColor()
label.font = UIFont.systemFontOfSize(17)
label.text = "text\(i)"
label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
sc.addSubview(label)
}
sc.contentSize = CGSize(width: count * width, height: 44)
view.addSubview(sc)
*/
letsc =createScrollView({ () ->Intin
return15
}) { (index) ->UILabelin
letwidth =50
letlabel =UILabel()
label.backgroundColor=UIColor.greenColor()
label.textColor=UIColor.darkGrayColor()
label.font=UIFont.systemFontOfSize(17)
label.text="text\(index)"
label.frame=CGRect(x: index * width, y:0, width: width, height:44)
returnlabel
}
view.addSubview(sc)
}
funccreateScrollView(labelCount: ()->Int, labelWithIndex: (index:Int)->UILabel) ->UIScrollView{
// 1.创建UIScrollView
letsc =UIScrollView(frame:CGRect(x:0, y:100, width:375, height:44))
letcount = labelCount()
//let width = 50
// 2.遍历创建UILabel
foriin0..
/*
let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.textColor = UIColor.darkGrayColor()
label.font = UIFont.systemFontOfSize(17)
label.text = "text\(i)"
label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
sc.addSubview(label)
*/
letlabel = labelWithIndex(index: i)
sc.addSubview(label)
sc.contentSize=CGSize(width:CGFloat(count) * label.bounds.width, height:44)
}
//返回UIScrollView
returnsc
}
}
Swift (闭包返回值)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 注:从我的CSDN博客转入failed to get the task for process 7518这一次碰到...