Добавить и удалить вид динамически iOS Swift - PullRequest
0 голосов
/ 31 марта 2020

enter image description here

if count != 0 {
    for i in stride(from: 0, to: Int(count!), by: 1) {
        if i==0 {
            let commodity = storeDetails?.storeSearchPickup.storeMaterialsDetails[i]
            let balesPicked: Int = (commodity?.balesPicked)!
            let balesRemaining: Int = (commodity?.balesRemaining)!
            commoditySection.balepickupedlabeltext.text = String(balesPicked)
            commoditySection.balesremaininglabeltext.text = String(balesRemaining)
            commoditySection.commodityLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
            commoditySection.commodityLabel.text = commodity?.materialShortName
            commoditySection.setCommodity(details: commodity!, index: Int((commodity?.materialId)!))
            commoditySection.callback = self
            commoditiesViewCount = commoditiesViewCount+1
            print("commoditiesViewCount\(commoditiesViewCount)")
            self.currentSelectedCommodity = commodity
        } else {
            let commodity = storeDetails?.storeSearchPickup.storeMaterialsDetails[i]
            var commodityView = CommoditySection(frame:CGRect(x: 0, y: 0, width: commoditycontainer.frame.width, height:   CGFloat.leastNonzeroMagnitude));
            self.commoditycontainer.addSubview(commodityView)
            commodityView.setCommodity(details: commodity!, index: Int((commodity?.materialId)!))
            commodityView.translatesAutoresizingMaskIntoConstraints = false
            commodityView.callback = self

            if(currentCommodityView != nil) {
                let verticalConstraint = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.currentCommodityView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
                let leading = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.currentCommodityView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
                let trailing = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.currentCommodityView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
                NSLayoutConstraint.activate([verticalConstraint,leading,trailing])
                commodityheightconstraint.constant = commodityheightconstraint.constant + commodityView.frame.height
                print("commoditiesViewCount\(commoditiesViewCount)")

            } else {
                let verticalConstraint = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.commoditySection, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
                let leading = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.commoditySection, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
                let trailing = NSLayoutConstraint(item: commodityView, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.commoditySection, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
                NSLayoutConstraint.activate([verticalConstraint,leading,trailing])
                commodityheightconstraint.constant =  commodityheightconstraint.constant + commodityView.frame.height

                print("commoditiesViewCount\(commoditiesViewCount)")

            }
            commodityView.commodityLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
            commodityView.commodityLabel.text = commodity?.materialShortName
            let balesPicked: Int = (commodity?.balesPicked)!
            let balesRemaining: Int = (commodity?.balesRemaining)!
            commodityView.balepickupedlabeltext.text = String(balesPicked)
            commodityView.balesremaininglabeltext.text = String(balesRemaining)
            self.currentCommodityView = commodityView;
            commoditiesViewCount = commoditiesViewCount+1
            print("commoditiesViewCount\(commoditiesViewCount)")

            isSecondViewAdded = true
        }
    }
}

Я новичок в iOS разработке, но опытный Android Разработчик У меня есть один контейнер, в который необходимо добавлять и удалять представления, когда Я добавляю первый вид, он правильно вписывается в контейнер, когда прагматически добавляем другой вид ниже первого, он покрывает другие виды под контейнером. Высота обоих видов равна CGFloat.leastNonzeroMagnitude. Необходимо установить высоту динамических элементов c в соответствии с содержащимися представлениями

.
...