1.UIButton
let btn = UIButton()
btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
btn.layer.cornerRadius = 10.P
btn.backgroundColor = .yellow
btn.setTitle("点击一下", for: .normal)
btn.setTitleColor(kRGB(51, 51, 51), for: .normal)
btn.titleLabel?.font = kFont(14)
self.addSubview(btn)
btn.snp.makeConstraints { make in
make.left.equalTo(self).offset(P(10))
make.right.equalTo(self).offset(-10.P)
make.width.equalTo(10.P)
make.height.equalTo(10.P)
}
2.UILabel
let titleLabel = UILabel()
titleLabel.text = "隐私协议"
titleLabel.font = kBFont(16)
titleLabel.textColor = kRGB(51, 51, 51)
self.addSubview(titleLabel)
titleLabel.snp.makeConstraints {(make) in
make.centerY.equalTo(iconImgV)
make.height.equalTo(16.P)
make.left.equalTo(iconImgV.snp.right).offset(6.P)
make.width.equalTo(70.P)
}
3.UIImageView
let imgV = UIImageView()
imgV.image = UIImage(named: "")
self.addSubview(imgV)
imgV.snp.makeConstraints { (make) in
make.left.equalTo(self).offset(10.P)
make.right.equalTo(self).offset(10.P)
make.width.equalTo(100.P)
make.height.equalTo(100.P)
}
4.UITextField
let textField = UITextField()
textField.text = "输入"
textField.placeholder = "轻呼入"
textField.font = kFont(14)
textField.textColor = .red
textField.backgroundColor = .yellow
textField.addTarget(self, action: #selector(textFieldChange(_:)), for: .editingChanged)
self.addSubview(textField)
textField.snp.makeConstraints {(make) in
make.left.equalTo(self).offset(10.P)
make.right.equalTo(self).offset(-10.P)
make.width.equalTo(100.P)
make.height.equalTo(100.P)
}
@objc private func textFieldChange(_ textField: UITextField) {
guard let _: UITextRange = textField.markedTextRange else{
if (textField.text! as NSString).length > 5 {
textField.text = (textField.text! as NSString).substring(to: 5)
}
return
}
}
5.UITableView
private func setupViews() {
let tableview = UITableView()
tableview.backgroundColor = .gray
tableview.delegate = self
tableview.dataSource = self
tableview.separatorStyle = .none
tableview.register(HomeTableCell.self, forCellReuseIdentifier: "cell")
tableview.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(refreshBottom))
tableview.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(refreshTop))
self.view.addSubview(tableview)
tableview.snp.makeConstraints { (make) in
make.top.equalTo(self.view)
make.left.right.equalTo(self.view)
make.bottom.equalTo(self.view)
}
}
@objc private func refreshTop() {
}
@objc private func refreshBottom() {
}
}
extension ViewController : UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.P
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var model = People.init()
model.name = "hhhhhhhhhhhh"
let cell : HomeTableCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableCell
cell.model = model
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
6.UICollectionView
private func setupViews() {
let layout = UICollectionViewFlowLayout.init()
layout.itemSize = CGSize(width: 100.P, height: 100.P)
layout.minimumLineSpacing = 10.P
layout.minimumInteritemSpacing = 10.P
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .gray
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CollecCell.self, forCellWithReuseIdentifier: "cell")
collectionView.register(CollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
collectionView.register(CollectionFootView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "foot")
collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(refreshTop))
collectionView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(refreshBottom))
self.view.addSubview(collectionView)
collectionView.snp.makeConstraints { (make) in
make.top.equalTo(self.view)
make.left.right.equalTo(self.view)
make.bottom.equalTo(self.view)
}
}
@objc func refreshTop() {
}
@objc func refreshBottom() {
}
}
extension HomeVC : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CollecCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollecCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 10.P, left: 10.P, bottom: 10.P, right: 10.P)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 0, height: 200.P)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: 0, height: 200.P)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let headerView:CollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! CollectionHeaderView
return headerView
} else {
let footerView:CollectionFootView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "foot", for: indexPath) as! CollectionFootView
return footerView
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}