错误 swift2 约束 SnapKit

Posted

技术标签:

【中文标题】错误 swift2 约束 SnapKit【英文标题】:Error swift2 constraints SnapKit 【发布时间】:2016-07-07 11:21:42 【问题描述】:

我正在使用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】:

我假设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。我想你应该决定一个,要么设置 centerY 要么设置顶部布局指南的偏移量,你应该很高兴。

【讨论】:

以上是关于错误 swift2 约束 SnapKit的主要内容,如果未能解决你的问题,请参考以下文章

更新约束不适用于 UITableView snapkit

SnapKit 自动布局损坏:“无法同时满足约束”

Swift SnapKit 动态高度问题

Swift 控件约束之 SnapKit

更新 SnapKit 约束偏移

SnapKit 更新约束导致冲突