简体   繁体   中英

Change UIView Layer - Swift

I try to implement UIView with sequence. With below code the output like in this picture. (This is normal behaviour)

在此处输入图片说明

But I want to line up like below picture

在此处输入图片说明

Elements sorted from right!

imageView.trailingAnchor.constraint(equalTo: participantView.safeAreaLayoutGuide.trailingAnchor, constant: CGFloat(-(counter * widthX - 5 * counter)) ).isActive = true

What can I do now?

   let widthX = 30
   var counter = 0
   for x in element {

          guard let image = x.image else { continue }
          let urlX = URL(string: "...")
          let imageView = UIImageView()
          participantView.addSubview(imageView)

          imageView.translatesAutoresizingMaskIntoConstraints = false
          imageView.trailingAnchor.constraint(equalTo: participantView.safeAreaLayoutGuide.trailingAnchor, constant: CGFloat(-(counter * widthX - 5 * counter)) ).isActive = true
          imageView.topAnchor.constraint(equalTo: participantView.topAnchor, constant: 0).isActive = true
          imageView.bottomAnchor.constraint(equalTo: participantView.bottomAnchor, constant: 0).isActive = true
          imageView.widthAnchor.constraint(equalToConstant: 30).isActive = true
          imageView.backgroundColor = .white

          imageView.layer.cornerRadius = 15
          imageView.clipsToBounds = true
          imageView.contentMode = .scaleAspectFit
          imageView.layer.borderWidth = 1
          imageView.layer.borderColor = UIColor.lightGray.cgColor
          counter += 1

          imageView.kf.setImage(with: urlX)
   }

Replace

participantView.addSubview(imageView)

with

participantView.insertSubview(imageView,at:0)

position of 1 ,2 ..etc (left/right alignment) depends on image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM