简体   繁体   English

更改UIView图层-Swift

[英]Change UIView Layer - Swift

I try to implement UIView with sequence. 我尝试用序列实现UIView。 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 1,2 ..etc(左/右对齐)的位置取决于图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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