Home > Blockchain >  Wrong layout inside cell
Wrong layout inside cell

Time:01-23

I'm trying to create a calendar with collection view but i have a small problem: wrong layout inside cells

Here my code for cell's ui configuration:

addSubview(dateLabel)
addSubview(moneyLabel)
        
NSLayoutConstraint.activate([
   dateLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 1),
   dateLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 1),
   dateLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -1),
            
   moneyLabel.topAnchor.constraint(equalTo: dateLabel.bottomAnchor, constant: 2),
   moneyLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 1),
   moneyLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -1)
])

How i can change constraints that center of both labels was in the middle of cell?

CodePudding user response:

You can use the following code for your problem.

contentView.addSubview(dateLabel)
contentView.addSubview(moneyLabel)

dateLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
dateLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true

Above code will centre your dateLabel and then you can centre other too. Also make sure that textAlignment property of UILabels is set to .center

  •  Tags:  
  • Related