Не удается найти решение в Интернете, но приведенный ниже код может дать вам представление о том, как его реализовать, используя UICollectionView
:
Создать UIViewcontroller
с UICollectionView
как подпредставление со всеми четырьмя ограничениями с нулевым значением.
В вашем UIViewcontroller
добавьте следующие коды:
import UIKit
class ViewController: UIViewController {
let arrayTitle = ["Hi", "Hello", "Ok", "Got it"]
let arrayColors = [UIColor.red,UIColor.blue,UIColor.black,UIColor.gray]
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView!.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cellReuseIdentifier")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellReuseIdentifier", for: indexPath) as! CustomCollectionViewCell
cell.labelTitle.text = arrayTitle[indexPath.row]
cell.backgroundColor = arrayColors[indexPath.row]
return cell
}
}
Создайте CustomCollectionViewCell
и добавьте следующие строки кода:
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var labelTitle: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
Надеюсь, вы получите представление о том, как это реализовать. Я думаю, что вы можете справиться с раскадровкой самостоятельно.
Если у вас есть какие-либо вопросы относительно раскадровки, реализации пользовательского интерфейса и ограничений, не стесняйтесь спрашивать.