2017-07-02 35 views
0

我有一個應用程序通過單擊按鈕從服務器下載圖像。圖像下載後,我創建一個新的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>:乘數爲零或零的第二個項目與第一個屬性的位置一起創建位置等於常量的非法約束。位置屬性必須成對指定。'

回答

1

每當你與scrollViews工作,有2條拇指規則是: -

  1. 給了滾動前置,底部和頂部約束相對於所述上海華,即self.view

    @IbOutlet weak var scrollView: UIScrollView! 
    
        let leading = NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1.0, constant: 0) 
    
        let trailing = NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1.0, constant: 0) 
    
        let top = NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 0) 
    
        let bottom = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0) 
    
  2. 相對於添加的限制的內容查看到滾動視圖

    @IbOutlet weak var contentView: UIView! 
    
    let leading = NSLayoutConstraint(item: contentView, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1.0, constant: 0) 
    let trailing = NSLayoutConstraint(item: contentView, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1.0, constant: 0) 
    
    let top = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0) 
    
        //increase the constant according to how much long you need the scrollview to be 
    let bottom = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0) 
    

現在對於添加子視圖的限制(標籤,圖像)的內容查看

對於示例 - 您收到您的第一形象,因此我們將保持的UIImageViews你的函數之外的數組。

var imageViews = [UIImageViews]() //declared outside the function 


    //received an image here 
     var imageView = UIImageView() // define the frame according to yourself using frame init method 
     imageView.image = image 

    if imageViews.isEmpty { // first image view 
    //add constraints to first image view 

    let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1.0, constant: 0) 
    let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, 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) 
    } 

else { //second, third, fourth image view... so on 

    let x = NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: contentView, attribute: .centerX, multiplier: 1.0, constant: 0) 
    let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: imageViews[imageViews.count - 1], attribute: .bottom, 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) 
} 

     imageViews.append(imageView) 
} 

希望你現在有了一個想法,該如何着手解決這個問題。如果具有超過4或5個圖像視圖,則可能需要檢查數組的計數並相應地增加scrollView的contentView。您可以通過使用 self.scrollView.contentSize = CGSize(width, height)

+0

Up vote。我有一個評論準備發表一些這樣的說法,但決定它更多的是一個答案。更多的是(從OP中隱含的其他東西)比我提供的「答案」 - 所以我只是沒有發佈任何東西。很好的解釋在UIScrollView中正確設置東西的各種部分 – dfd

+0

謝謝.. ....:) –

+0

你是偉大的,謝謝))我認爲這將是我很多時間瞭解是否有其他限制我自己)) – sergs

0

我相信你在這裏有一個問題:

let y = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 30) 

「toItem」 參數應該有一定的價值。另外第一個參數應該是imageView.topAnchor。也許這應該是這個樣子:

let y = NSLayoutConstraint(item: imageView.topAnchor, attribute: .top, relatedBy: .equal, toItem: self.contentView.topAnchor, attribute: .notAnAttribute, multiplier: 1.0, constant: 30) 
+0

做到這一點,並沒有幫助( – sergs

+0

2017年7月2日15:42:51.208 ImageFromServerTest [12886:1114489 ***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因:' NSLayoutConstraint for :未知的佈局屬性' – sergs