Reputation: 133
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
.
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).
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
Reputation: 4409
Try to add UIPageControl
to viewController
's view
.
Replace
collectionView.addSubview(pageControl)
With
self.view.addSubview(pageControl)
Upvotes: 0