Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIPageControl with dot images

When I needed to custom my UIPageControl I used this and this solution.

Slightly modifying it for the new version of swift we have :

class myPageControl: UIPageControl {
    var activeImage: UIImage!
    var inactiveImage: UIImage!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        activeImage = UIImage(named: "active.png")!
        inactiveImage = UIImage(named: "inactive.png")!
    }

    func updateDots()
    {
        for i in 0 ..< self.subviews.count
        {
            let dot : UIImageView = imageViewForSubview(self.subviews[i])
            if (i == self.currentPage) {
                dot.image = activeImage
            }
            else {
                dot.image = inactiveImage
            }
        }
    }

    func imageViewForSubview(view: UIView) -> UIImageView {
        var dot: UIImageView? = nil
        if (view.isKindOfClass(UIView)) {
            for subview: UIView in view.subviews {
                if (subview is UIImageView) {
                    dot = (subview as! UIImageView)
                }
            }
            if dot == nil {
                dot = UIImageView(frame: CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height))
                view.addSubview(dot!)
            }
        }
        else {
            dot = (view as! UIImageView)
        }
        return dot!
    }

    func setPage(page: Int) {
        super.currentPage = page
        self.updateDots()
    }
}

My problem is that I can not change the picture when you first start app. It only change when the page is changed.

In viewDidLoad(), I added setPage(0) and updateDots(), but there is no result. What could i be doing wrong?

like image 566
Mikhail Sein Avatar asked Nov 28 '22 13:11

Mikhail Sein


2 Answers

I improved @CodenameDuchess's answer a bit. You can also customize the images on xibs/storyboards.

class CustomPageControl: UIPageControl {

@IBInspectable var currentPageImage: UIImage?

@IBInspectable var otherPagesImage: UIImage?

override var numberOfPages: Int {
    didSet {
        updateDots()
    }
}

override var currentPage: Int {
    didSet {
        updateDots()
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    pageIndicatorTintColor = .clear
    currentPageIndicatorTintColor = .clear
    clipsToBounds = false
}

private func updateDots() {

    for (index, subview) in subviews.enumerated() {
        let imageView: UIImageView
        if let existingImageview = getImageView(forSubview: subview) {
            imageView = existingImageview
        } else {
            imageView = UIImageView(image: otherPagesImage)

            imageView.center = subview.center
            subview.addSubview(imageView)
            subview.clipsToBounds = false
        }
        imageView.image = currentPage == index ? currentPageImage : otherPagesImage
    }
}

private func getImageView(forSubview view: UIView) -> UIImageView? {
    if let imageView = view as? UIImageView {
        return imageView
    } else {
        let view = view.subviews.first { (view) -> Bool in
            return view is UIImageView
        } as? UIImageView

        return view
    }
}
}
like image 30
Politta Avatar answered Dec 01 '22 04:12

Politta


Swift 3.0 ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".

You will have to call "updateDots()" in viewDidAppear() and your valueChanged handler for the page control.

 import UIKit

 class CustomImagePageControl: UIPageControl {

   let activeImage:UIImage = UIImage(named: "SelectedPage")!
   let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!

   override func awakeFromNib() {
         super.awakeFromNib()

         self.pageIndicatorTintColor = UIColor.clear
         self.currentPageIndicatorTintColor = UIColor.clear
         self.clipsToBounds = false
    }

    func updateDots() {
         var i = 0
         for view in self.subviews {
             if let imageView = self.imageForSubview(view) {
                 if i == self.currentPage {
                     imageView.image = self.activeImage
                 } else {
                     imageView.image = self.inactiveImage
                 }
                 i = i + 1
             } else {
                 var dotImage = self.inactiveImage
                 if i == self.currentPage {
                     dotImage = self.activeImage
                 }
                 view.clipsToBounds = false
                 view.addSubview(UIImageView(image:dotImage))
                 i = i + 1
             }
         }
     }

     fileprivate func imageForSubview(_ view:UIView) -> UIImageView? {
         var dot:UIImageView?

         if let dotImageView = view as? UIImageView {
             dot = dotImageView
         } else {
             for foundView in view.subviews {
                 if let imageView = foundView as? UIImageView {
                     dot = imageView
                     break
                 }
             }
         }

         return dot
     }
 }
like image 193
CodenameDuchess Avatar answered Dec 01 '22 04:12

CodenameDuchess