6

How can I vertical-center a UIImageView in super view with the snapkit? I tried like this:

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

        }

but seems not well.How can I use the snapkit

Z.Q
  • 103
  • 1
  • 1
  • 8
  • I guess you forgot to add `make.centerX.equalTo(view.center.x)` part. And there are still 2 other constraints needed - `width` and `height`. Because it knows where the center should be. But does not know how big should be your image view – Pavel Stepanov Jul 13 '18 at 12:01
  • make.centerY.equalTo(view.center.y); just make the button of the imageview in the vertical-center of its superview – Z.Q Jul 13 '18 at 12:14

3 Answers3

11

Try following code

Imageview.snp.makeConstraints { (make) in
     make.left.equalTo(view.snp.left).offset(50)
     make.centerY.equalTo(self.view)
     make.width.height.equalTo(100)
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
Bhavesh.iosDev
  • 924
  • 9
  • 27
2

It's not enough to give the imageView a centerY only , you need to give it also width,height and x constraint , so try this

imageview.snp.makeConstraints { (make) in
      make.left.equalTo(view.snp.left).offset(50)
      make.centerY.equalTo(self.view)
      make.width.height.equalTo(100)
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

If you want to set imageView in center of its superview with size of 100x100 then try below code;

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

If you want to set imageView in vertial-center and with 50px offset from leading edge of its superview with size of 100x100 then try below code;

imgView.snp.makeConstraints { (make) in
    make.centerY.equalToSuperview()
    make.leading.equalToSuperview().offset(50)
    make.height.width.equalTo(100)
}
KTPatel
  • 1,212
  • 4
  • 19
  • 24