0

I tried to make a programatically view inside UICollectionViewCell, and I think it will be wise to put the "programatically view code" inside UICollectionViewCell.

So here is my code:

override func awakeFromNib() {
        super.awakeFromNib()

        let view = UIView()
        view.frame = CGRect.zero
        view.backgroundColor = UIColor.black

        self.contentView.addSubview(view)
        self.contentView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0),
            view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0),
            view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 25),
            view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 25),
            ])

        self.contentView.layoutIfNeeded()
    }

I was wondering why the auto layout does not work, instead if I use manual CGRect, the view did appeared.

3 Answers 3

1

This Solve it just needs to set view.translatesAutoresizingMaskIntoConstraints = false not The contenView

override func awakeFromNib() {
    super.awakeFromNib()

    let view = UIView()
    view.frame = CGRect.zero
    view.backgroundColor = UIColor.black


    self.contentView.addSubview(view)
    view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0),
        view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0),
        view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 25),
        view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 25),
        ])
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try to change self.contentView.translatesAutoresizingMaskIntoConstraints = false to: view.translatesAutoresizingMaskIntoConstraints = false

You'd like to turn off the translatesAutoresizingMaskIntoConstraints of your view since we want to apply autolayout to it.

Comments

1

First: You should not call layoutIfNeeded method, the auto layout cycle will do this for you after you change the rules (constraints)

Second: You should set translatesAutoresizingMaskIntoConstraints property to false for the view that will be layed out, so you should not do this

self.contentView.translatesAutoresizingMaskIntoConstraints = false

but you should do this

view.translatesAutoresizingMaskIntoConstraints = false

That's because autoresizing mask constraints are applied to your view with constraints that you've set. This may and probably will cause constraints break.

If this won't help then provide console output with autolayout errors.

1 Comment

noted. the thing that layoutIfNeeded is pretty crucial , thanks man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.