Я не использую раскадровку. Мой класс здесь;
import UIKit
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
setupViews()
//register collectionview
collectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
}
let topView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.white
return view
}()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = UIColor.orange
return cv
}()
lazy var button: UIButton = {
let btn = UIButton(type: .system)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Buton", for: .normal)
btn.backgroundColor = UIColor.orange
btn.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
return btn
}()
@objc func handleButton() {
print(1212)
}
func setupViews() {
view.addSubview(topView)
topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
topView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
topView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
topView.heightAnchor.constraint(equalToConstant: 250).isActive = true
view.addSubview(button)
button.topAnchor.constraint(equalTo: topView.topAnchor, constant: 15).isActive = true
button.centerXAnchor.constraint(equalTo: topView.centerXAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(equalToConstant: view.frame.width - 15).isActive = true
button.heightAnchor.constraint(equalToConstant: 50).isActive = true
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 10).isActive = true
collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
collectionView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height * 0.2)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let trial = Trial()
self.navigationController?.pushViewController(trial, animated: true)
}
}
class AppCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame:frame)
backgroundColor = UIColor.purple
setupViews()
}
lazy var button: UIButton = {
let btn = UIButton(type: .system)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("Buton", for: .normal)
btn.backgroundColor = UIColor.orange
btn.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
return btn
}()
@objc func handleButton() {
//statusShow.showSettings()
}
func setupViews() {
addSubview(button)
button.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
button.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0).isActive = true
button.widthAnchor.constraint(equalToConstant: self.frame.width * 0.3).isActive = true
button.heightAnchor.constraint(equalToConstant: self.frame.height * 0.2).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class Trial: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}