UIPageControl 自定义类 - 发现 nil 将图像更改为点 [英] UIPageControl custom class - found nil changing image to the dots

查看:34
本文介绍了UIPageControl 自定义类 - 发现 nil 将图像更改为点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自定义图像而不是普通点来实现 UIPageControl.所以我创建了一个自定义类并通过故事板连接它.从 ios7 开始,UIPageControl 的子视图包含 UIView 而不是 UIImageView.结果 UIView(UIIpageControl subviews) 的子视图不包含任何子视图,所以我收到错误:

I need to implement a UIPageControl with custom images instead the normal dot. So I create a custom class and connect it through the storyboard. Since ios7 the subview of UIPageControl contain UIView instead of UIImageView. The subviews of the resulting UIView(UIIpageControl subviews) doesn't contain any subviews so I receive the error:

致命错误:在展开可选值时意外发现 nil.

我可能哪里错了?

class WhitePageControl:UIPageControl{
   let activeImage = UIImage(named: "dot_white")
   let inactiveImage = UIImage(named: "dot_white_e")

   override init(frame: CGRect){
     super.init(frame: frame)
   }

   required init(coder aDecoder: NSCoder){
     super.init(coder: aDecoder)
   }

   func updateDots(){
     println(self.subviews.count) // 3
     println(self.numberOfPages) // 3
     for var index = 0; index < self.subviews.count; index++ {
        println(index)
        var dot:UIImageView!
        var dotView:UIView = self.subviews[index] as UIView
        println("1")
        for subview in dotView.subviews{ // NIL HERE
            println("2")
            if subview.isKindOfClass(UIImageView){
                println("3")
                dot = subview as UIImageView
                if index == self.currentPage{ dot.image = activeImage }
                else{ dot.image = inactiveImage }
            }
            }
         }
       }

    func setCurrentPage(value:Int){
        super.currentPage = value
        self.updateDots()
    }
}

推荐答案

这是我的解决方案:

import Foundation

class PageControl: UIPageControl {
    var activeImage: UIImage!
    var inactiveImage: UIImage!
    override var currentPage: Int {
        //willSet {
        didSet { //so updates will take place after page changed
            self.updateDots()
        }
    }

    convenience init(activeImage: UIImage, inactiveImage: UIImage) {
        self.init()

        self.activeImage = activeImage
        self.inactiveImage = inactiveImage

        self.pageIndicatorTintColor = UIColor.clearColor()
        self.currentPageIndicatorTintColor = UIColor.clearColor()
    }

    func updateDots() {
        for var i = 0; i < count(subviews); i++ {
            var view: UIView = subviews[i] as! UIView
            if count(view.subviews) == 0 {
                self.addImageViewOnDotView(view, imageSize: activeImage.size)
            }
            var imageView: UIImageView = view.subviews.first as! UIImageView
            imageView.image = self.currentPage == i ? activeImage : inactiveImage
        }
    }

    // MARK: - Private

    func addImageViewOnDotView(view: UIView, imageSize: CGSize) {
        var frame = view.frame
        frame.origin = CGPointZero
        frame.size = imageSize

        var imageView = UIImageView(frame: frame)
        imageView.contentMode = UIViewContentMode.Center
        view.addSubview(imageView)
    }

}

这篇关于UIPageControl 自定义类 - 发现 nil 将图像更改为点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆