![](https://img.haomeiwen.com/i1397370/ce7c328e7a09fbc4.png)
图纸
![](https://img.haomeiwen.com/i1397370/e462c40632a0a332.png)
实物
import UIKit
class ViewController: UIViewController {
let W = UIScreen.mainScreen().bounds.width //屏幕宽度
let H = UIScreen.mainScreen().bounds.height //屏幕高度
let allView = 20 // 全部视图个数
let mainViewW:CGFloat = 70 //视图宽度
let mainViewH:CGFloat = 100 //视图高度
let totalloc = 4 //列数
override func viewDidLoad() {
super.viewDidLoad()
let margin = (W - mainViewW * CGFloat(totalloc))/5 //边缘
for i in 0..<allView {
let loc = i%totalloc //列数
let row = i/totalloc //行数(1/3=0,2/3=0,3/3=1)
let viewX = margin + (margin + mainViewW) * CGFloat(loc)
let viewY = margin + (margin + mainViewH) * CGFloat(row)
// 创建主视图
let mainView = UIView(frame: CGRect(x: viewX, y: viewY, width: mainViewW, height: mainViewH))
self.view.addSubview(mainView)
// 添加子视图
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 80))
//imageView.image = UIImage(named: "")
//imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.backgroundColor = UIColor(red: 126/255, green: 211/255, blue: 33/255, alpha: 1)
mainView.addSubview(imageView)
// 添加label
let label = UILabel(frame: CGRect(x: 0, y: 80, width: 70, height: 20))
label.text = "Label"
label.textAlignment = .Center
label.font = UIFont.systemFontOfSize(12)
label.backgroundColor = UIColor(red: 216/255, green: 211/255, blue: 33/255, alpha: 1)
mainView.addSubview(label)
}
}
如何增加及判断组件的交互事件:详情进入——>
网友评论