Tien Ho
Tien Ho

Reputation: 133

ios: Vertical position is ambiguous for UIPageControl

I have a UIPageControl as a subview of a UICollectionView. Here are the constraints for the UICollectionView:

let collectionViewConstraints = [
        collectionView.topAnchor.constraint(equalTo: view.topAnchor),
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        collectionView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.65),
        collectionView.widthAnchor.constraint(equalTo: view.widthAnchor)
    ]

collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(collectionViewConstraints)

Constraints for the UIPageControl are:

collectionView.addSubview(pageControl)
let pageControlConstraints = [
        pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        pageControl.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor, constant: 100)
    ]

pageControl.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(pageControlConstraints)

Since pageControl has intrinsicContentSize, I only specify its x and y positions. However, I encounter an ambiguous layout error for the vertical position of pageControl.

enter image description here

As shown in the first screenshot, the pageControl doesn't seem to get pinned to the bottom of collectionView. When debugging the view hierarchy, it seems that the bottom edge of pageControl is laid out in respect to the bottom edge of collectionView, yet the distance between the two seems much larger than what I specified (100).

enter image description here enter image description here

I'm not sure what causes this ambiguous layout since I believe I have specified all the necessary constraints for pageControl. I would appreciate any feedback.

Note: the ambiguity goes away if I constrain pageControl to collectionView.topAnchor instead of collectionView.bottomAnchor.

Upvotes: 1

Views: 528

Answers (1)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4409

Try to add UIPageControl to viewController's view.

Replace

collectionView.addSubview(pageControl)

With

self.view.addSubview(pageControl)

Upvotes: 0

Related Questions