【问题标题】:Swift SnapKit how to layout UIView with relative positionSwift SnapKit 如何用相对位置布局 UIView
【发布时间】:2016-11-11 02:51:58
【问题描述】:
func setupPosition() {
        box.snp.makeConstraints{(make)->Void in
        make.edges.equalTo(view).inset(UIEdgeInsetsMake(64+20, 20, 250, 20))
        }

        textField.snp.makeConstraints{(make)->Void in
            make.edges.equalTo(box).inset(UIEdgeInsetsMake(5, 5, 150, 5))

        }

        stackBoxOne.snp.makeConstraints{(make)->Void in
                make.top.equalTo(box).offset(textField.frame.size.height)
                make.left.equalTo(box).offset(5)
                make.bottom.equalTo(box).offset(-90)
                make.right.equalTo(box).offset(-5)
        }
    }

我想将stackBoxOne 放在textField 下。但是上面的代码不起作用。如何修改代码?感谢您的时间。

【问题讨论】:

    标签: swift snapkit


    【解决方案1】:

    您不能在 makeConstraints 闭包内使用 textField.frame,因为此时尚未布局视图。因此,高度为0,而stackBoxOne 的偏移量为0

    要将视图放置在另一个视图的下方,请将其顶部约束连接到另一个视图的底部约束。所以在你的情况下:

    stackBoxOne.snp.makeConstraints{(make)->Void in
         make.top.equalTo(textField.snp.bottom)
         make.left.equalTo(box).offset(5)
         make.bottom.equalTo(box).offset(-90)
         make.right.equalTo(box).offset(-5)
    }
    

    除此之外,您还可以将左右约束设置为等于textFields 左右约束,如下所示:

    stackBoxOne.snp.makeConstraints{(make)->Void in
        make.top.equalTo(textField.snp.bottom)
        make.left.right.equalTo(textField)
        make.bottom.equalTo(box).offset(-90)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多