How do I implement UIPageControl in Swift

后端 未结 4 1562
春和景丽
春和景丽 2021-02-02 10:30

Ok so I\'m struggling here and haven\'t been able to find a working solution. I\'ve been self learning Swift without Objective C experience (I know, I know).

In my app,

4条回答
  •  臣服心动
    2021-02-02 11:04

    Just setup it in code like this:

    private var pageControl = UIPageControl(frame: .zero)
    
    private func setupPageControl() {
    
        pageControl.numberOfPages = controllers.count
        pageControl.translatesAutoresizingMaskIntoConstraints = false
        pageControl.currentPageIndicatorTintColor = UIColor.orange
        pageControl.pageIndicatorTintColor = UIColor.lightGray.withAlphaComponent(0.8)
    
        let leading = NSLayoutConstraint(item: pageControl, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0)
        let trailing = NSLayoutConstraint(item: pageControl, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)
        let bottom = NSLayoutConstraint(item: pageControl, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
    
        view.insertSubview(pageControl, at: 0)
        view.bringSubview(toFront: pageControl)
        view.addConstraints([leading, trailing, bottom])
    }
    

提交回复
热议问题