首页 > 解决方案 > 如何复制元素

问题描述

问候同事我正在尝试复制带有数字 1234 的蓝色框,但我做不到,您可以给我任何指导。

在此处输入图像描述

这是我制作视图的部分

final class IBANInvesOptView: UIView {
    let container: UIView = UIView()
    let stackView: UIStackView = UIStackView()
    
    
    private let products: UIScrollView = {
        let products = UIScrollView()
        products.isDirectionalLockEnabled = true
        products.isScrollEnabled = true
        products.bounces = true
        products.canCancelContentTouches = false
        products.delaysContentTouches = true
        products.alwaysBounceVertical = true
        products.showsHorizontalScrollIndicator = false
        products.alwaysBounceHorizontal = false
        return products
    }()
    
    let imageView: UIImageView = UIImageView()
    
    private let lblTitle: UILabel = {
        let lblTitle = UILabel().usingAutoLayout()
        lblTitle.textAlignment = .center
        lblTitle.numberOfLines = 3
        lblTitle.adjustsFontSizeToFitWidth = true
        lblTitle.attributedText = "pipeline.title".localized().bold40(.newBlue)
        return lblTitle
    }()
    
    private let lblDescription: UILabel = {
        let lblDescription = UILabel().usingAutoLayout()
        lblDescription.numberOfLines = 3
        lblDescription.adjustsFontSizeToFitWidth = true
        lblDescription.attributedText = "pipeline.subtitle".localized().book20(.newBlue)
        return lblDescription
    }()
    
    let stackPipe: UIStackView = {
        let stackPipe = UIStackView().usingAutoLayout()
        stackPipe.axis = .vertical
        stackPipe.alignment = .fill
        stackPipe.isLayoutMarginsRelativeArrangement = true
        stackPipe.layoutMargins = UIEdgeInsets(top: 30, left: 15, bottom: 30, right: 15)
        return stackPipe
    }()
    
    
    private let lblAgree: UILabel = {
        let lblAgree = UILabel().usingAutoLayout()
        lblAgree.numberOfLines = 3
        lblAgree.adjustsFontSizeToFitWidth = true
        lblAgree.attributedText = "pipeline.confirm".localized().book15(.newBlue)
        return lblAgree
    }()
    
    let checkBox: Checkbox = Checkbox()
    
    let stackViewBtns: UIStackView = UIStackView()
    let disposeBag: DisposeBag = DisposeBag()
    var buttons = [UILabel()]


    
    override init(frame: CGRect) {
        super.init(frame: frame)
        pipeline()
        listSubviewsOfView()
        for item in 1...5 {
           print("alguna vez entra?")
            self.products.addSubview(self.stackPipe)
            self.stackPipe.addArrangedSubview(lblTitleCompo)
             buttons.append(lblTitleCompo)
            constrain(lblTitleCompo) {
                lblTitleCompo in
                if let superview = lblTitleCompo.superview {
                    
                    lblTitleCompo.top ==  superview.top - 10
                    lblTitleCompo.leading == superview.leading + 10
                    lblTitleCompo.trailing == superview.trailing - 10
                }
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    let lblTitleCompo: UILabel = {
        let lblTitleCompo = UILabel().usingAutoLayout()
        lblTitleCompo.textAlignment = .center
        lblTitleCompo.numberOfLines = 3
        lblTitleCompo.adjustsFontSizeToFitWidth = true
        lblTitleCompo.attributedText = "12345".localized().book20(.slateGray)
        return lblTitleCompo
    }()
    
    func listSubviewsOfView() {
    for item in 1...5 {
       print("alguna vez entra?")
        self.products.addSubview(self.stackPipe)
        self.stackPipe.addArrangedSubview(lblTitleCompo)
         buttons.append(lblTitleCompo)
        constrain(lblTitleCompo) {
            lblTitleCompo in
            if let superview = lblTitleCompo.superview {
                
                lblTitleCompo.top ==  superview.top - 10
                lblTitleCompo.leading == superview.leading + 10
                lblTitleCompo.trailing == superview.trailing - 10
            }
        }
    }
}
    

    

    private func setupView() {
        backgroundColor = .clear
        self.addBlurToBackground()

        self.container.backgroundColor = .red
        self.container.layer.cornerRadius = 8

        self.stackView.axis = .vertical
        self.stackView.alignment = .center
        self.stackView.distribution = .fillProportionally
        self.stackView.spacing = 15
        self.stackView.backgroundColor = .blue
        
        self.products.backgroundColor = .yellow
        self.products.layer.cornerRadius = 8
        
        self.stackPipe.roundedCornersAndShadow()
        self.stackPipe.backgroundColor = .newBlue
        
        self.checkBox.isSelected = false

        self.imageView.contentMode = .center
        self.imageView.clipsToBounds = true

        self.stackViewBtns.axis = .horizontal
        self.stackViewBtns.alignment = .center
        self.stackViewBtns.distribution = .fillEqually
        self.stackViewBtns.spacing = 9

        
       
        addSubview(self.container)
        self.container.addSubview(self.stackView)
        self.container.addSubview(self.products)
        self.container.addSubview(self.lblAgree)
        self.container.addSubview(self.checkBox)
        self.stackView.addArrangedSubview(self.imageView)
        self.stackView.addArrangedSubview(self.lblTitle)
        self.stackView.addArrangedSubview(self.lblDescription)
        self.container.addSubview(self.stackViewBtns)
        self.products.addSubview(self.stackPipe)
   
        self.constrainViews()
    }

    private func addBlurToBackground() {
        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(blurEffectView)
    }

    private func constrainViews() {
        constrain(self.container, self.stackView, self.stackViewBtns, self.products, self.checkBox, self.stackPipe, self.lblAgree) {
            container, stackView, stackViewBtns, products, checkBox, stackPipe, lblAgree in
            if let superview = container.superview {
                container.centerY == superview.centerY
                container.leading == superview.leading + 20
                container.trailing == superview.trailing - 20
                container.top ==  superview.top + 40
                container.bottom == superview.bottom - 40
            }

            stackView.top == container.top + 10
            stackView.leading == container.leading + 5
            stackView.trailing == container.trailing - 5
            
            products.top == stackView.bottom + 25
            products.leading == container.leading + 5
            products.trailing == container.trailing - 5
            products.height == 460
            
            stackPipe.top == products.top + 25
            stackPipe.leading == container.leading + 10
            stackPipe.trailing == container.trailing - 10
            stackPipe.height == 100
            
            lblAgree.top == stackViewBtns.top - 60
            lblAgree.leading == checkBox.trailing + 10
            lblAgree.trailing == container.trailing - 20

            checkBox.top == stackViewBtns.top - 50
            checkBox.leading == container.leading + 20

            stackViewBtns.leading >= container.leading + 15
            stackViewBtns.trailing <= container.trailing - 15
            stackViewBtns.centerX == container.centerX
            stackViewBtns.bottom == container.bottom - 20
        }
        
        constrain(self.lblTitle, self.lblDescription) {
            lblTitle, lblDescription in
            if let superview = lblTitle.superview {
                
                lblTitle.top ==  superview.top + 10

                lblDescription.top ==  lblTitle.bottom + 5
                lblDescription.leading == superview.leading + 85
                lblDescription.trailing == superview.trailing - 85
            }
        }
        constrain(self.imageView) {
            imageView in
            imageView.height == 200
            imageView.width == 80 ~ UILayoutPriority(750)
        }
    }

    func build(title: String,
               description: String?,
               image: UIImage?,
               buttons: [IBANInvesOptButtonConfiguration]) -> Observable<Int> {


        let createdBtns = buttons
            .map { createButton(with: $0) }

        createdBtns
            .forEach { stackViewBtns.addArrangedSubview($0) }

        self.setupView()
        setNeedsLayout()

        let obsBtns = createdBtns
            .enumerated()
            .map { index, element in
                element.rx.tap.asObservable().map { index }
            }

        return Observable.from(obsBtns).merge()  
        
    }

  

这就是我管理视图的方式,您可以检查我试图复制元素的 for 循环

标签: iosswiftfor-loopuiview

解决方案


    override init(frame: CGRect) {
        super.init(frame: frame)
        pipeline()
        listSubviewsOfView()
        for item in 1...5 {
            print("alguna vez entra?")
            self.products.addSubview(self.stackPipe)
            self.stackPipe.addArrangedSubview(lblTitleCompo)

以上没有多大意义。您正在添加stackPipelblTitleCompo多次。一个视图只能有一个父视图。

如果您想要五个 stackPipes 和五个 lblTitleCompos,则必须制作其中五个。

而不是使用letmake afunc来创建您的视图...

func makeLabelTitle() -> UILabel {
    let lblTitle = UILabel().usingAutoLayout()
    lblTitle.textAlignment = .center
    lblTitle.numberOfLines = 3
    lblTitle.adjustsFontSizeToFitWidth = true
    lblTitle.attributedText = "pipeline.title".localized().bold40(.newBlue)
    return lblTitle
}

每次调用上述内容时,它都会创建一个新标签。


推荐阅读