2016-07-07 92 views
0

我使用SnapKit到約束添加到圖片我有一個UIViewController錯誤swift2約束SnapKit

我有這樣的代碼:

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:[email protected]/Users/usuario/Swift/TestApp/TestAppV1/TestViewController.swift#62 UIImageView:0x7f84297789e0.centerY == UIView:0x7f84295e36b0.centerY>", 
    "<SnapKit.LayoutConstraint:[email protected]/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:[email protected]/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

我假設imagenPrincipal是你的圖像視圖,以後可以在這些約束看出:

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

如果我讀這正確,圖像的頂部設置到頂部佈局引導,通過偏移5分,然後您將centerY設置爲視圖的中心,然後將圖像的高度設置爲50.我認爲您應該選擇一個,要麼設置centerY,要麼從頂部佈局指南設置偏移量,你應該很好走。