I have some code where I'm adding in a "close" UIButton
to the UIPageController.view
, and that works great.
WORKING CODE:
class func closeButton(_ pageController: UIPageViewController) -> UIButton {
let button = UIButton()
button.setTitleColor(UIColor.white, for: UIControlState())
button.setTitle("Close", for: UIControlState())
button.titleLabel!.font = UIFont.systemFont(ofSize: 15)
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
button.translatesAutoresizingMaskIntoConstraints = false
pageController.view.addSubview(button)
button.widthAnchor.constraint(equalToConstant: 50).isActive = true
button.heightAnchor.constraint(equalToConstant: 36).isActive = true
button.trailingAnchor.constraint(equalTo: pageController.view.trailingAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: pageController.view.bottomAnchor).isActive = true
return button
}
But then I tested my code on the iPhone X Simulator, and the UIPageControl
is much higher from the bottom than the other iPhones. So now that close button I just added is in the bottom rounded corner and the UIPageControl
is much higher.
So then I tried referencing the the UIPageControl
with:
let pageControl: UIPageControl = UIPageControl.appearance()
And then replace the button.bottomAnchor.constraint...
with:
button.centerYAnchor.constraint(equalTo: pageControl.centerYAnchor).isActive = true
But this causes an error to be thrown. I tried printing the UIPageControl.frame
and it is 0,0,0,0. So I'm assuming that has something to do with it.
Is there a way to make a constraint against the UIPageControl?
If you are using a custom view try to update your constraint in layoutSubviews() method, or in viewDidLayoutSubviews() method of your view controller:
func layoutSubviews() {
super.layoutSubviews()
button.centerYAnchor.constraint(equalTo: pageControl.centerYAnchor).isActive = true
}