我有一個應用程序通過單擊按鈕從服務器下載圖像。圖像下載後,我創建一個新的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