我有一个应用程序通过单击按钮从服务器下载图像。图像下载后,我创建一个新的imageView并将其添加到我的contentView(UIView)。我需要创建的限制 - 每一个新的ImageView需要从以前的一个我需要顶级约束的未知元素
func addNewImageToTheScrollView(img: UIImage?) {
if let imageResponse = img {
let imageView = UIImageView(image: imageResponse.crop(rect: CGRect(x: 0, y: imageResponse.size.height/2, width: self.contentView.frame.width, height: 200)))
self.contentView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1.0, constant: 0)
let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30)
let width = NSLayoutConstraint(item: imageView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1.0, constant: 0)
let height = NSLayoutConstraint(item: imageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150)
self.contentView.addConstraints([x, y])
imageView.addConstraints([width, height])
}
}
顶部约束。如果我评论约束代码,这将是做工精细,除非每一个新的ImageView将在同一个地方,在顶部风景。现在下载后白衣这个约束代码,我有这样的代码问题
2017年7月2日14:50:01.018 ImageFromServerTest [11516:1080948 ***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:' NSLayoutConstraint for>:乘数为零或零的第二个项目与第一个属性的位置一起创建位置等于常量的非法约束。位置属性必须成对指定。'
Up vote。我有一个评论准备发表一些这样的说法,但决定它更多的是一个答案。更多的是(从OP中隐含的其他东西)比我提供的“答案” - 所以我只是没有发布任何东西。很好的解释在UIScrollView中正确设置东西的各种部分 – dfd
谢谢.. ....:) –
你是伟大的,谢谢))我认为这将是我很多时间了解是否有其他限制我自己)) – sergs