Josh Hutchins
Josh Hutchins

Reputation: 1

Use of unresolved identifier 'view'

Trying to add constraints to a collection view. Keep getting the error, "Use of unresolved identifier 'view'" Any help is much appreciated.

import UIKit

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
       // cv.backgroundColor = UIColor.white
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    let cellId = "cellId"

    override init(frame:CGRect) {
        super.init(frame: frame)

        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)


        addSubview(self.collectionView)
       collectionView.translatesAutoresizingMaskIntoConstraints = false

        collectionView.leadingAnchor.constraint(
            equalTo: view.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(
            equalTo: view.trailingAnchor).isActive = true
        collectionView.topAnchor.constraint(
            equalTo: view.topAnchor,
            constant: -20).isActive = true
        collectionView.heightAnchor.constraint(
            equalTo: view.heightAnchor,
            multiplier: 0.10).isActive = true

        backgroundColor = UIColor.red

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)

        cell.backgroundColor = .blue

        return cell
    }

Upvotes: 0

Views: 1725

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100533

There is no view property in a UIView, it's a property of UIViewController.

Since self is the view in this case, replace:

equalTo: view.leadingAnchor).isActive = true

with:

equalTo: self.leadingAnchor).isActive = true

and same for other ones.

NSLayoutConstraint.activate([
    collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
    collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
    collectionView.topAnchor.constraint(equalTo: self.topAnchor,constant: -20),
    collectionView.heightAnchor.constraint(equalTo: self.heightAnchor,multiplier: 0.10)
)]

Upvotes: 1

Jogendar Choudhary
Jogendar Choudhary

Reputation: 3494

You need to replace view with self because this is not viewcontroller this is MenuView so yo need to use self for that:

    collectionView.leadingAnchor.constraint(
        equalTo: self.leadingAnchor).isActive = true
    collectionView.trailingAnchor.constraint(
        equalTo: self.trailingAnchor).isActive = true
    collectionView.topAnchor.constraint(
        equalTo: self.topAnchor,
        constant: -20).isActive = true
    collectionView.heightAnchor.constraint(
        equalTo: self.heightAnchor,
        multiplier: 0.10).isActive = true

Upvotes: 0

Related Questions