带有snapkit的垂直居中UIImageView

时间:2018-07-13 11:58:19

标签: swift snapkit

如何使用snapkit在超级视图中垂直居中放置UIImageView? 我这样尝试过:

 imageview.snp.makeConstraints { (make) in
            make.centerY.equalTo(view.center.y);

        }

但看起来不太好。我该如何使用Snapkit

3 个答案:

答案 0 :(得分:3)

尝试以下代码

Imageview.snp.makeConstraints { (make) in

            make.centerY.centerX.equalTo(self.view)
            make.width.height.equalTo(100)
}

答案 1 :(得分:0)

仅给imageView提供一个centerY是不够的,您还需要赋予它width,height和x约束,所以尝试一下

imageview.snp.makeConstraints { (make) in
            make.top.equalTo(self.view.snp.top).offset(50)
            make.centerY.equalTo(self.view)
            make.width.height.equalTo(100)
}

答案 2 :(得分:0)

如果要将imageView设置为其superview的中心,尺寸为100x100,请尝试以下代码;

imageview.snp.makeConstraints { (make) in
    make.centerY.centerX.equalToSuperview()
    make.height.width.equalTo(100) 
}

如果您想将imageView设置为垂直中心,并且距其父视图的前边缘偏移50px,尺寸为100x100,则请尝试以下代码;

imgView.snp.makeConstraints { (make) in
    make.centerY.equalToSuperview()
    make.leading.equalToSuperview().offset(50)
    make.height.width.equalTo(100)
}