首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift ios使用循环为多个子视图添加约束

swift ios使用循环为多个子视图添加约束
EN

Stack Overflow用户
提问于 2016-07-30 21:13:15
回答 1查看 1.3K关注 0票数 2

我是swift的新手,尝试学习如何使用循环添加几个子视图及其约束。我试着遵循一些类似问题的答案,但它不起作用。你能帮我写代码吗?提前谢谢你。

下面是我的代码:

代码语言:javascript
复制
    let card = UIView()

    let view2 = UIView()

    view2.backgroundColor = UIColor.greenColor()

    view2.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(view2)

    let leftSideConstraint3 = NSLayoutConstraint(item: view2, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: 0.0)
    let topConstraint3 = NSLayoutConstraint(item: view2, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0.0)
    let widthConstraint3 = NSLayoutConstraint(item: view2, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0.0)
    let heightConstraint3 = NSLayoutConstraint(item: view2, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: 0.0)
    view.addConstraints([leftSideConstraint3, topConstraint3, heightConstraint3, widthConstraint3])



    var cards = [UIView](count: 16, repeatedValue: card) // array with 16 cards



    card.layer.borderWidth = 3
    card.layer.borderColor = UIColor.blackColor().CGColor
    card.backgroundColor = UIColor.orangeColor()



    var columnCounter:Int = 0
    var rowCounter:Int = 0


    //Loop through each card in the array
    for index in 0...cards.count-1 {



        // place the card in the view and turn off translateAutoresizingMask
        let thisCard = cards[index]
        thisCard.translatesAutoresizingMaskIntoConstraints = false
        view2.addSubview(thisCard)

        //set the height and width constraints
        let widthConstraint = NSLayoutConstraint(item: thisCard, attribute: .Width, relatedBy: .Equal, toItem: view2, attribute: .Width, multiplier: 0.25, constant: 0)
        let heightConstraint = NSLayoutConstraint(item: thisCard, attribute: .Height, relatedBy: .Equal, toItem: view2, attribute: .Height, multiplier: 0.25, constant: 0)
        view2.addConstraints([heightConstraint, widthConstraint])

        //set the horizontal  position

        if (columnCounter > 0) {

            // card is not in the first column
            let cardOnTheLeft = cards[index-1]

            let leftSideConstraint = NSLayoutConstraint(item: thisCard, attribute: .Left, relatedBy: .Equal, toItem: cardOnTheLeft, attribute: .Right, multiplier: 1, constant: 0)

            //add constraint to the contentView
            view2.addConstraint(leftSideConstraint)

        } else {

            //card is in the first column

            let leftSideConstraint = NSLayoutConstraint(item: thisCard, attribute: .Left, relatedBy: .Equal, toItem: view2, attribute: .Left, multiplier: 1, constant: 0)

            //add constraint to the contentView
            view2.addConstraint(leftSideConstraint)

        }


        //set the vertical position

        if (rowCounter > 0) {

            // card is not in the first row
            let cardOnTop = cards[index-4]

            let topConstraint = NSLayoutConstraint(item: thisCard, attribute: .Top, relatedBy: .Equal, toItem: cardOnTop, attribute: .Bottom, multiplier: 1, constant: 0)

            // add constraint to the contentView
            view2.addConstraint(topConstraint)

        } else {

            //card is in the first row

            let topConstraint = NSLayoutConstraint(item: thisCard, attribute: .Top, relatedBy: .Equal, toItem: view2, attribute: .Top, multiplier: 1, constant: 0)

            //add constraint to the contentView
            view2.addConstraint(topConstraint)

        }



        //increment the column counter
        columnCounter = columnCounter+1

        //if the column counter reaches the fifth column reset it and increase the row counter
        if (columnCounter >= 4) {
            columnCounter = 0
            rowCounter = rowCounter+1
        }




    } // end of the loop
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-30 21:26:28

这将创建一个具有16个对同一张卡的引用的数组。

代码语言:javascript
复制
var cards = [UIView](count: 16, repeatedValue: card)

将其替换为:

代码语言:javascript
复制
var cards = (1...16).map { _ in UIView() }

这将导致具有16个唯一UIView的阵列。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38674322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档