错误swift2约束SnapKit

时间:2016-07-07 11:21:42

标签: swift constraints snapkit

我使用SnapKit为图片添加约束我有一个UIViewController

我有这段代码:

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)
}

问题是我没有将图像大小调整到我想要的大小,而控制台却向我显示了这个错误:

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.

任何可能的解决方案?

我使用另一个库来进行约束?

谢谢!

1 个答案:

答案 0 :(得分:1)

我假设imagenPrincipal是您的图片视图,稍后可以看到这些约束:

 "<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>",

如果我正确读取,请将图像顶部设置为顶部布局指南,偏移5个点,然后将centerY设置为视图的中心,然后将图像的高度设置为50。我想你应该决定一个,要么设置中心Y或偏离顶部布局指南,你应该好好去。