//MARK: NSLayoutConstraints
//trailing
let trailingConstaint = NSLayoutConstraint(item: imageView, attribute: .trailing, relatedBy: .equal, toItem:imageView.superview , attribute: .trailing, multiplier: 1, constant: 0)
trailingConstaint.isActive = true
//leading
let leadingConstraint = NSLayoutConstraint(item: imageView, attribute: .leading, relatedBy: .equal, toItem: imageView.superview, attribute: .leading, multiplier: 1, constant: 0)
leadingConstraint.isActive = true
//top
let topConstraint = NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
//bottom
let bottomConstraint = NSLayoutConstraint(item: imageView, attribute: .bottom, relatedBy: .equal, toItem: imageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
bottomConstraint.isActive = true我想要调整图像的大小,使其适合于适当地缩放图像ViewController截图。
但我知道这个错误..。
由于NSInvalidArgumentException异常而终止应用程序,原因是:“大小为{746,496}的NSLayoutConstraint为0标度1.000000:约束项必须是UIView或UILayoutGuide的实例。”
发布于 2018-01-26 07:41:26
//top
let topConstraint = NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true您将此约束设置为映像,而不是imageView。
例外情况清楚地表明,Constraint items must each be an instance of UIView, or UILayoutGuide.'
所有其他约束都是在imageView.superView,、、imageView、之间设置的,因此,从逻辑上讲,上述约束也应该使用相同的对。
这将处理例外情况。
//top
let topConstraint = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true但是,图像缩放不能通过约束来实现。您必须适当地设置contentMode属性。检查了解图像如何在解释得很好的医生。中缩放部分
发布于 2018-01-26 07:42:03
看起来,您引用的是图像,而不是topConstraint中的imageView。如果没有看到更多的代码,就很难确定,但是尝试更新它,看看它是否有效。
let topConstraint = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true发布于 2018-01-26 07:33:54
在第三个约束中,您使用了image而不是imageView。不能对图像设置约束,只能在视图上设置约束。看看您是如何设置imageView的。
您为什么不简单地设置Interface中的约束呢?他们相当直截了当.
可以设置contentMode属性以确定图像的缩放方式。scaleAspectFill通常是您想要的,尽管如果它不符合视图的话,这会导致图像的某些裁剪。scaleAspectFit将确保显示整个图像,但如果图像大小与视图大小不匹配,则需要进行一些填充。scaleToFill将填充视图而不填充,可能会以扭曲图像为代价。
https://stackoverflow.com/questions/48456677
复制相似问题