UIView of a UIViewController ambiguous position when created programmatically

Viewed 222

I get a black screen for my UIViewController. The reason as I understood from the debugger is ambiguous position for UIView inside the UIViewController.

I have created a tagList programmatically the scenario is like this:

I have a UIStackView. Inside the stackView I add the arranged subview which is an UIImage and a UIViewController. Inside the UIViewController there is a UICollectionView. The UIViewController must have a fixed size. I gave all the needed constraints :

Initialising the tagList

lazy var tagBarView: TopBarViewController = {
    let view = TopBarViewController(nibName: nil, bundle: nil)
    view.view.translatesAutoresizingMaskIntoConstraints = false
    view.view.backgroundColor = .blue
    view.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 48)
    view.view.clipsToBounds = true
    return view
}()

calling the tagList

self.stackView.insertSubview(self.tagBarView.view, at: 1)

constraints of the UIStackView

stackView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    
    
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
stackView.spacing = 0
stackView.clipsToBounds = false

Then again I created the collectionView inside my viewController programmatically this is the setup for the collectionView and FlowLayout

setup for the collectionView and FlowLayout

func setupUI() {
    let collectionViewFlowLayout = UICollectionViewFlowLayout()
    collectionViewFlowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    collectionViewFlowLayout.scrollDirection = .horizontal
    topBarCollectionView = UICollectionView(frame: CGRect(x: 8, y: 8, width: 604, height: 32), collectionViewLayout: collectionViewFlowLayout)
    topBarCollectionView.clipsToBounds = true
    topBarCollectionView.contentMode = .scaleToFill
    topBarCollectionView.showsVerticalScrollIndicator = false
    topBarCollectionView.showsHorizontalScrollIndicator = false
    
    topBarCollectionView.clearsContextBeforeDrawing = false
    topBarCollectionView.register(TagCollectionViewCell.self, forCellWithReuseIdentifier: "TagCell")
    topBarCollectionView.collectionViewLayout = collectionViewFlowLayout
    topBarCollectionView.contentInsetAdjustmentBehavior = .always
    topBarCollectionView.collectionViewLayout.invalidateLayout()
    self.topBarCollectionView.delegate = self
    self.topBarCollectionView.dataSource = self
    topBarCollectionView.translatesAutoresizingMaskIntoConstraints = false
    
}

The constraints for the UICollectioView

func setupConstraints() {

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

  
    self.view.safeAreaLayoutGuide.trailingAnchor.constraint(equalTo: topBarCollectionView.trailingAnchor, constant: 8).isActive = true

    self.view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: topBarCollectionView.bottomAnchor, constant: 8).isActive = true
    
    self.view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: topBarCollectionView.leadingAnchor, constant: -8).isActive = true
    self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: topBarCollectionView.topAnchor, constant: -8).isActive = true

    topBarCollectionView.heightAnchor.constraint(equalToConstant: 32).isActive = true
    topBarCollectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 16).isActive = true

    
    topBarCollectionView.reloadData()
}

The result is that in my hierarchy the UIView of the UIViewController is not properly positioned so I get a black screen.

View Hierarchy View Hierar

The Final Result The view is the black one on the top(which should be white) enter image description here

1 Answers

I finally found the answer it doesn't matter if you at first give your views a frame. to make them show up you have to always have a height constraint on all the view in your screen.

Related