0

I am using UIPageControl in my application & setting constraint like -

var trailingAnchor:CGFloat = -16
        if #available(iOS 14, *) {
            trailingAnchor = 16
        }
 trailing = pageControl.trailingAnchor.constraint(equalTo: contentContainerView.trailingAnchor, constant: trailingAnchor)

Unfortunately i have to write trailingAnchor:CGFloat based on iOS version otherwise the pagecontrol getting outside the View in iOS 13 & According to this from iOS 14 we must have to give constraint to pageControl So can i get rid of var trailingAnchor?

Jack
  • 13,571
  • 6
  • 76
  • 98
  • That doesn't seem to make sense... are you also setting a Leading or Width constraint? Can you show your full set of constraints, and images of the difference you're getting between iOS 13 and 14 (using the same trailing value, not the adjustment you're showing here)? – DonMag May 04 '21 at 16:13
  • @DonMag if i am using `Trailing` , `Top`, & `fix Width` but strange behaviour observed as above. – Jack May 04 '21 at 17:02

1 Answers1

0

For UIPageControl there are appearance and functionality differences, but as far as layout goes the only difference should be the intrinsic height.

Here is a simple example... add a "container" UIView, and add a page control as a subview of that container:

class ViewController: UIViewController {
    
    let contentContainerView = UIView()
    let pageControl = UIPageControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        contentContainerView.translatesAutoresizingMaskIntoConstraints = false
        pageControl.translatesAutoresizingMaskIntoConstraints = false
        
        contentContainerView.addSubview(pageControl)
        view.addSubview(contentContainerView)
        
        // respect safe area
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            
            contentContainerView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            contentContainerView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            contentContainerView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            
            contentContainerView.heightAnchor.constraint(equalToConstant: 120.0),
            
            pageControl.leadingAnchor.constraint(equalTo: contentContainerView.leadingAnchor, constant: 16.0),
            pageControl.trailingAnchor.constraint(equalTo: contentContainerView.trailingAnchor, constant: -16.0),
            pageControl.bottomAnchor.constraint(equalTo: contentContainerView.bottomAnchor, constant: -8.0),

        ])

        contentContainerView.backgroundColor = .systemYellow
        pageControl.backgroundColor = .systemTeal
        
        pageControl.numberOfPages = 7
    }
    
}

Here's how it looks between iOS 14 (on the left) and iOS 13 (on the right):

enter image description here

DonMag
  • 69,424
  • 5
  • 50
  • 86