在ViewController中,我有一个Button和一个CollectionView。 CollectionView被“固定”在ViewController的底部,并且Button应该在CollectionView的正上方。当我单击Button时,CollectionView应该扩展其高度,并且Button应该再次向上移动到CollectionView的正上方。当我再次单击Button时,CollectionView应该恢复到原始大小,并且Button也再次向下移动。同样,当CollectionView展开时,它应该显示更多信息,而不会显示,但是以某种方式,CollectionViewCell的大小会更新,而不是其中的数据。
首先,我尝试在发生错误的ViewController上使用self.reloadData()。然后,我尝试了self.viewDidLoad(),该操作没有给出错误,但是CollectionView在相同的位置上,没有移动,而Button向上移动,但是第二次单击Button时,什么也没有发生。我正在使用按钮标签来了解collectionView是否展开。
我也收到此警告:
"<NSLayoutConstraint:0x6040002949b0 UIButton:0x7fd6ca419890.bottom == UIView:0x7fd6ca508970.bottom - 98 (active)>",
"<NSLayoutConstraint:0x604000480e60 UIButton:0x7fd6ca419890.bottom == UIView:0x7fd6ca508970.bottom - 318 (active)>"
"<NSLayoutConstraint:0x6040002944b0 UICollectionView:0x7fd6cc081200.height == 100 (active)>",
"<NSLayoutConstraint:0x60000028e5b0 UICollectionView:0x7fd6cc081200.height == 320 (active)>"
ViewController:
import UIKit
class TicketsController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, TicketsControllerDelegate {
lazy var mainCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
cv.dataSource = self
cv.delegate = self
return cv
}()
let expandButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor(displayP3Red: 242/255, green: 242/255, blue: 242/255, alpha: 1)
button.tintColor = UIColor(displayP3Red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 2.0
button.tag = 0
return button
}()
lazy var informationCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 0/255, alpha: 0.05)
cv.layer.borderColor = UIColor.lightGray.cgColor
cv.layer.borderWidth = 2.0
cv.dataSource = self
cv.delegate = self
return cv
}()
let ticketsCellId = "TicketsCellId"
let informationCellId = "InformationCellId"
let categorys: [Categorys] = {
return [Categorys(title: "Kategorie 1", category: "Sitzplatz", oldPrice: "74,90€", newPrice: "59,92€", amount: "0"),
Categorys(title: "Kategorie 2", category: "Stehplatz", oldPrice: "66,90€", newPrice: "53,52€", amount: "0"),
Categorys(title: "Kategorie 3", category: "Stehplatz", oldPrice: "58,90€", newPrice: "47,12€", amount: "0")]
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
navigationController?.navigationBar.barTintColor = UIColor(displayP3Red: 124/255, green: 217/255, blue: 97/255, alpha: 1)
let backButton = UIBarButtonItem(image: UIImage(named: "back icon"), style: .plain, target: self, action: #selector(handleBackButton))
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.tintColor = UIColor.white
navigationItem.title = "Tickets"
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
navigationController?.navigationBar.titleTextAttributes = textAttributes
view.backgroundColor = .white
view.addSubview(mainCollectionView)
view.addSubview(expandButton)
view.addSubview(informationCollectionView)
mainCollectionView.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: informationCollectionView.topAnchor, right: view.rightAnchor)
if expandButton.tag == 1 {
_ = expandButton.anchor(nil, left: nil, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 200 + (CGFloat(TicketsController().categorys.count) - 1 ) * (50 + 10) - 2, rightConstant: 32, widthConstant: 25, heightConstant: 25)
_ = informationCollectionView.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 200 + (CGFloat(TicketsController().categorys.count) - 1 ) * (50 + 10))
}
else {
_ = expandButton.anchor(nil, left: nil, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 100 - 2, rightConstant: 32, widthConstant: 25, heightConstant: 25)
_ = informationCollectionView.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 100)
}
mainCollectionView.register(TicketsCell.self, forCellWithReuseIdentifier: ticketsCellId)
informationCollectionView.register(InformationCell.self, forCellWithReuseIdentifier: informationCellId)
expandButton.addTarget(self, action: #selector(handleExpandButton), for: .touchUpInside)
if expandButton.tag == 1 {
expandButton.setImage(UIImage(named: "dropdown down icon")?.withRenderingMode(.alwaysTemplate), for: .normal)
}
else {
expandButton.setImage(UIImage(named: "dropdown up icon")?.withRenderingMode(.alwaysTemplate), for: .normal)
}
}
@objc func handleBackButton() {
navigationController?.popViewController(animated: true)
}
@objc func handleExpandButton() {
print(expandButton.tag)
if expandButton.tag == 1 {
expandButton.tag = 0
reloadData()
self.viewDidLoad()
}
else {
expandButton.tag = 1
reloadData()
self.viewDidLoad()
}
}
@objc func handleNextButton() {
print("Next")
navigationController?.pushViewController(AdressController(), animated: true)
}
func showTicketAmountWindow() {
TicketAmountController().showTicketAmountWindow()
}
func reloadData() {
mainCollectionView.reloadData()
informationCollectionView.reloadData()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.mainCollectionView {
let cell = mainCollectionView.dequeueReusableCell(withReuseIdentifier: ticketsCellId, for: indexPath) as! TicketsCell
cell.ticketsControllerDelegate = self
return cell
}
else {
let cell = informationCollectionView.dequeueReusableCell(withReuseIdentifier: informationCellId, for: indexPath) as! InformationCell
cell.ticketsControllerDelegate = self
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == self.mainCollectionView {
return CGSize(width: view.frame.width, height: 190 + CGFloat(TicketsController().categorys.count * (50 + 15)))
}
else {
if expandButton.tag == 1 {
return CGSize(width: view.frame.width, height: 200 + (CGFloat(TicketsController().categorys.count) - 1 ) * (50 + 10))
}
else {
return CGSize(width: view.frame.width, height: 100)
}
}
}
}
CollectionViewCell:
import UIKit
class InformationCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let titleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 15)
label.text = "Ihre Bestellung"
label.textColor = UIColor(displayP3Red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
return label
}()
lazy var orderCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 15
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
cv.dataSource = self
cv.delegate = self
return cv
}()
let seperationLabel: SeperationLineBlack = {
let label = SeperationLineBlack()
return label
}()
let finalPriceLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 15)
label.text = "Ihr Preis"
label.textColor = UIColor(displayP3Red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
return label
}()
let priceAsNumberLabel: UILabel = {
let label = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 15)
label.text = "134,30€"
label.textColor = UIColor(displayP3Red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
return label
}()
let nextButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor(displayP3Red: 139/255, green: 221/255, blue: 116/255, alpha: 1)
button.setTitle("Weiter", for: .normal)
button.layer.cornerRadius = 20.0
return button
}()
let orderCellId = "OrderCellId"
weak var ticketsControllerDelegate: TicketsControllerDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
addSubview(titleLabel)
addSubview(orderCollectionView)
addSubview(seperationLabel)
addSubview(finalPriceLabel)
addSubview(priceAsNumberLabel)
addSubview(nextButton)
if TicketsController().expandButton.tag == 1 {
_ = titleLabel.anchor(nil, left: leftAnchor, bottom: orderCollectionView.topAnchor, right: nil, topConstant: 0, leftConstant: 16, bottomConstant: 5, rightConstant: 0, widthConstant: 0, heightConstant: 0)
_ = orderCollectionView.anchor(nil, left: leftAnchor, bottom: seperationLabel.topAnchor, right: rightAnchor, topConstant: 0, leftConstant: 16, bottomConstant: 8, rightConstant: 16, widthConstant: 0, heightConstant: CGFloat(TicketsController().categorys.count) * (50 + 10))
_ = seperationLabel.anchor(nil, left: leftAnchor, bottom: finalPriceLabel.topAnchor, right: rightAnchor, topConstant: 0, leftConstant: 16, bottomConstant: 8, rightConstant: 16, widthConstant: 0, heightConstant: 2)
}
_ = finalPriceLabel.anchor(nil, left: leftAnchor, bottom: nextButton.topAnchor, right: nil, topConstant: 0, leftConstant: 16, bottomConstant: 16, rightConstant: 0, widthConstant: 0, heightConstant: 0)
_ = priceAsNumberLabel.anchor(nil, left: finalPriceLabel.rightAnchor, bottom: nextButton.topAnchor, right: rightAnchor, topConstant: 0, leftConstant: 16, bottomConstant: 16, rightConstant: 16, widthConstant: 0, heightConstant: 0)
_ = nextButton.anchor(nil, left: nil, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 16, rightConstant: 16, widthConstant: 100, heightConstant: 40)
orderCollectionView.register(OrderCell.self, forCellWithReuseIdentifier: orderCellId)
nextButton.addTarget(self, action: #selector(handleNextButton), for: .touchUpInside)
}
@objc func handleNextButton() {
ticketsControllerDelegate?.handleNextButton()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return TicketsController().categorys.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = orderCollectionView.dequeueReusableCell(withReuseIdentifier: orderCellId, for: indexPath) as! OrderCell
let setCell = TicketsController().categorys[indexPath.item]
cell.orderCell = setCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width - 16 - 16, height: 50)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}