UIPageControl not visible in iOS 14
if UIPageControl not visible in iOS 14 then simply add a width constraint and assign relation "greater that or equal" or make increase width of UIPageControl.
UIPageControl works a little differently in iOS 14. It might be surprising that it does not show anything if the width is too small.
import UIKit
class ViewController: UIViewController {
lazy var label: UILabel = UILabel()
var widthConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
let pageControl = UIPageControl()
pageControl.numberOfPages = 10
pageControl.currentPage = 3
pageControl.pageIndicatorTintColor = .red
pageControl.currentPageIndicatorTintColor = .green
pageControl.layer.borderWidth = 1
pageControl.layer.borderColor = UIColor.purple.cgColor
widthConstraint = pageControl.widthAnchor.constraint(equalToConstant: 100)
widthConstraint.isActive = true
let slider = UISlider()
slider.minimumValue = 0
slider.maximumValue = 300
slider.value = 100
slider.widthAnchor.constraint(equalToConstant: 200).isActive = true
slider.addTarget(self, action: #selector(sliderValueDidChange(_:)), for: .valueChanged)
let stackView = UIStackView(arrangedSubviews: [label, pageControl, slider])
stackView.axis = .vertical
stackView.alignment = .center
stackView.spacing = 40
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
stackView.widthAnchor.constraint(equalTo: view.widthAnchor)
])
updateLabel()
}
@objc
func sliderValueDidChange(_ sender: Any?) {
guard let slider = sender as? UISlider else { return }
widthConstraint.constant = CGFloat(slider.value)
updateLabel()
}
func updateLabel() {
if #available(iOS 14.0, *) {
label.text = "iOS 14 PageControl width: \(widthConstraint.constant.rounded())"
} else {
label.text = "iOS 13 PageControl width: \(widthConstraint.constant.rounded())"
}
}
}