Error constraints swift 2 with SnapKit

0

I'm using SnapKit to add constraints to an image I have in 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 resize the image to the dimensions that I want and the console shows me this error that I do not know why:

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 solutions?

Should I use another library for the constraints ??

Thank you!

    
asked by 07.07.2016 в 13:17
source

2 answers

0

instead of doing a makeconstraints what it does is modify existing constraints, try to remakeContraints what it does is build the constraints again, if this does not work is that the constraints that you are indicating are not correct and enter into a conflict that makes him petar

    
answered by 08.07.2016 / 17:30
source
0

The problem is that the upper side of your UIImageView is anchored to the top margin of the view:

<_UILayoutSupportConstraint:0x7f84297c3af0 V:|-(0)-[_UILayoutGuide:0x7f84295427b0]   (Names: '|':UIView:0x7f84295e36b0 )>

And it also has the center anchored to the center of the view:

<SnapKit.LayoutConstraint:0x7f8429792d10@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.centerY == UIView:0x7f84295e36b0.centerY>

This implicitly creates a height for the view, but then you specify a height of 50.0 :

<SnapKit.LayoutConstraint:0x7f84297119d0@/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.height == 50.0>

There's the conflict in your Constraints.

I guess your intention is to center the image and give it a height of 50.0 .
I recommend you create a reference to the Constraint of the upper border between view and image and before applying your Constraints uninstall this constraint.

    
answered by 28.07.2016 в 20:58