Я бы хотел показывать фотографии пользователей моего приложения так же, как Facebook в своих группах. Я хотел бы начать с массива users_id и добавить изображения в зависимости от количества пользователей, присоединившихся к этой группе. Пока я попал сюда:
//this is the array of users id
addImagesWithArray(imageName:["139","140","138"])
func addImagesWithArray(imageName:[String]){
var imagesArray = [UIImageView]()
// I append the imaged inside the for loop
for (index, _) in imageName.enumerated(){
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 25
imageView.layer.masksToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.borderWidth = 1.0
imageView.image = UIImage(named: imageName[index])
imagesArray.append(imageView)
}
//this is easy, I add the first image on the superview center
let firstImage = imagesArray.first
self.containingView.addSubview(firstImage!)
firstImage!.heightAnchor.constraint(equalToConstant: 50).isActive = true
firstImage!.widthAnchor.constraint(equalToConstant: 50).isActive = true
firstImage!.centerXAnchor.constraint(equalTo: self.containingView.centerXAnchor).isActive = true
firstImage!.topAnchor.constraint(equalTo: self.groupNameLabel.bottomAnchor).isActive = true
firstImage!.bottomAnchor.constraint(equalTo: self.containingView.bottomAnchor).isActive = true
if imagesArray.count > 1 {
var leftAnchor:NSLayoutConstraint?
for i in 1 ..< imagesArray.count {
let imageView = imagesArray[i]
self.containingView.addSubview(imageView)
imageView.heightAnchor.constraint(equalToConstant: 50).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
imageView.topAnchor.constraint(equalTo: firtImage!.topAnchor).isActive = true
if i == 1 {
// the second image constraint is based on the first image center
leftAnchor = imageView.leftAnchor.constraint(equalTo: firstImage!.centerXAnchor,constant: 4)
leftAnchor?.isActive = true
}else{
// THE PROBLEM IS HERE, I thought I could set the subsequent left ancors based on the position of each new UIImageViews but as you can see, the third images goes to the far left.
leftAnchor = imageView.leftAnchor.constraint(equalTo: imageView.centerXAnchor,constant: 4)
leftAnchor?.isActive = true
}
}
}
}