我有一個tableview單元的子類,它需要在其中有一個UICollectionView。我正在嘗試添加UICollectionView時面臨問題。它讓我添加一個新的UICollectionView,但我無法設置其約束。下面是我的代碼做同樣的:在Swift中以編程方式製作UICollectionView
// Add Collection
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
collectionProducts = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
contentView.addSubview(collectionProducts)
let leadingColConstraint = NSLayoutConstraint(item: collectionProducts, attribute: NSLayoutAttribute.leadingMargin, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.leadingMargin, multiplier: 1, constant: 0)
let trailingColConstraint = NSLayoutConstraint(item: collectionProducts, attribute: NSLayoutAttribute.trailingMargin, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.trailingMargin, multiplier: 1, constant: 0)
let topColConstraint = NSLayoutConstraint(item: collectionProducts, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: lblHeader, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 62)
let bottomColConstraint = NSLayoutConstraint(item: collectionProducts, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 55)
contentView.addConstraints([leadingColConstraint,trailingColConstraint,topColConstraint,bottomColConstraint])
collectionProducts.translatesAutoresizingMaskIntoConstraints = false
我錯過了什麼?我是Swift相對較晚的人,也是編程式創建單元的新手。我可以很容易地使用筆尖或故事板來做同樣的事情,但我被困在這裏。請幫忙出去。
乾草@Mayank Pahuja檢查此鏈接https://開頭WWW。 youtube.com/watch?v=RxEGlpeONOc其幫助完整 –