I have a CollectionView and I want to size it's height dynamical with the content. I get an error because the setted constraint in the storyboard conflicts with the constraint setted in my code:
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myCollectionView)
//other constraints of collectionView
collHeight = myCollectionView.heightAnchor.constraint(equalToConstant: 276)
collHeight!.isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//other stuff
collHeight!.constant = collectionView.contentSize.height
view.layoutIfNeeded()
}
Error: https://pastebin.com/5yQh5hUP I can't delete the constraint with Entf on it. It only set's the number to 0. Can I deactivate it programmatically?
You have two options:
Instead of creating your constraint in the viewDidLoad
function, you can make an outlet of the constraint and just set change it's constant.
@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myCollectionView)
// Set the constant
collectionViewHeightConstraint.constant = 276 // Change this as you wish
}