Search code examples
swiftconstraintssnapkit

Error swift2 constraints SnapKit


I 'm using SnapKit to add constraints to a picture I have a UIViewController

I have this code:

imagenPrincipal.contentMode = .ScaleToFill
let URL = NSURL(string: oferta!.imagen)!
let placeholderImage = UIImage(named: "placeholder")!
imagenPrincipal.sd_setImageWithURL(URL, placeholderImage: placeholderImage, completed: nil)

let altoNuevo = 120.0
let anchoNuevo = 230.0

print(imagenPrincipal.frame.size.height)//->140.0
imagenPrincipal.frame.size.height = CGFloat(altoNuevo)
print(imagenPrincipal.frame.size.height)//->120.0

imagenPrincipal.snp_makeConstraints { (make) -> Void in
    make.center.equalTo(self.view)
    make.height.equalTo(imagenPrincipal.frame.size.height)
}

The problem is that I do not resizes the image to the size I want and the console shows me this error that it not:

2016-07-07 13:12:44.652 TestApp[12517:197564] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<_UILayoutSupportConstraint:0x7f84297c21d0 V:[_UILayoutGuide:0x7f84295427b0(0)]>",
    "<_UILayoutSupportConstraint:0x7f84297c3af0 V:|-(0)-[_UILayoutGuide:0x7f84295427b0]   (Names: '|':UIView:0x7f84295e36b0 )>",
    "<NSLayoutConstraint:0x7f8429719520 V:[_UILayoutGuide:0x7f84295427b0]-(5)-[UIImageView:0x7f84297789e0]>",
    "<SnapKit.LayoutConstraint:0x7f8429792d10@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.centerY == UIView:0x7f84295e36b0.centerY>",
    "<SnapKit.LayoutConstraint:0x7f84297119d0@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.height == 50.0>",
    "<NSLayoutConstraint:0x7f84295f9480 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f84295e36b0(568)]>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x7f84297119d0@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.height == 50.0>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Any possible solution ?

I use another library for constraints ??

Thank you!


Solution

  • I'm assuming imagenPrincipal is your image view, which can later be seen on these constraints:

     "<NSLayoutConstraint:0x7f8429719520 V:[_UILayoutGuide:0x7f84295427b0]-(5)-[UIImageView:0x7f84297789e0]>",
     "<SnapKit.LayoutConstraint:0x7f8429792d10@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.centerY == UIView:0x7f84295e36b0.centerY>",
     "<SnapKit.LayoutConstraint:0x7f84297119d0@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.height == 50.0>",
    

    If I read this correctly, set the top of the image to the top layout guide, offset by 5 points, then you set the centerY to the center of the view, and then you set the height of the image to 50. I'm thinking you should decide on one, either set the centerY or the offset from the top layout guide, and you should be good to go.