Ios UIPageControl自定义类-发现无将图像更改为点

Ios UIPageControl自定义类-发现无将图像更改为点,ios,swift,null,uipagecontrol,Ios,Swift,Null,Uipagecontrol,我需要实现一个带有自定义图像的UIPageControl,而不是普通点。因此,我创建了一个自定义类,并通过故事板将其连接起来。 由于ios7,UIPageControl的子视图包含UIView,而不是UIImageView。生成的UIView(UIIpageControl子视图)的子视图不包含任何子视图,因此我收到错误: 致命错误:在展开可选值时意外发现nil 我可能错在哪里 class WhitePageControl:UIPageControl{ let activeImage = U

我需要实现一个带有自定义图像的
UIPageControl
,而不是普通点。因此,我创建了一个自定义类,并通过故事板将其连接起来。 由于ios7,UIPageControl的子视图包含
UIView
,而不是
UIImageView
。生成的
UIView
UIIpageControl
子视图)的子视图不包含任何子视图,因此我收到错误:

致命错误:在展开可选值时意外发现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()
    }
}
类WhitePageControl:UIPageControl{
让activeImage=UIImage(名为:“点白”)
让inactiveImage=UIImage(命名为:“点白”)
重写初始化(帧:CGRect){
super.init(frame:frame)
}
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
func updateDots(){
println(self.subviews.count)//3
println(self.numberOfPages)//3
对于var index=0;index
这是我的解决方案:

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)
    }

}
<代码>导入基础 类PageControl:UIPageControl{ var-activeImage:UIImage! var inactiveImage:UIImage! 覆盖当前页面:Int{ //意志{ didSet{//,因此更新将在页面更改后进行 self.updateDots() } } 便利初始化(activeImage:UIImage,inactiveImage:UIImage){ self.init() self.activeImage=activeImage self.inactiveImage=inactiveImage self.pageIndicatorTintColor=UIColor.clearColor() self.currentPageIndicatorTintColor=UIColor.clearColor() } func updateDots(){ 对于变量i=0;i这是我的解决方案:

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)
    }

}
<代码>导入基础 类PageControl:UIPageControl{ var-activeImage:UIImage! var inactiveImage:UIImage! 覆盖当前页面:Int{ //意志{ didSet{//,因此更新将在页面更改后进行 self.updateDots() } } 便利初始化(activeImage:UIImage,inactiveImage:UIImage){ self.init() self.activeImage=activeImage self.inactiveImage=inactiveImage self.pageIndicatorTintColor=UIColor.clearColor() self.currentPageIndicatorTintColor=UIColor.clearColor() } func updateDots(){ 对于变量i=0;i
将setCurrentPage更改为当前页面,因为其中一个是Obj C属性

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

试试这个。

将setCurrentPage更改为当前页面,因为其中一个是Obj C属性

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

尝试此操作。

找到解决方案了吗?找到解决方案了吗?当滚动到self.currentPage时,出现问题。我滚动到第1页,但self.currentPage仍然为0。对于使用自定义UIView并将ImageView和标签作为页码的页面控件,您有何建议?可能吗?对我也有帮助…谢谢。我已对其进行了修改(@IBDesignable)所以我可以在故事板中使用它。效果很好:-)@JamesLaurenstin你能分享它吗(@IBDesignable)在此处,当滚动到self.currentPage不正确时。我滚动到第1页,但self.currentPage仍然为0。对于使用自定义UIView并将ImageView和标签作为页码的页面控件,您有什么建议?可能吗?对我也有效…谢谢。我已对其进行了修改(@IBDesignable)所以我可以在故事板中使用它。效果很好:-)@JamesLaurenstin请在这里分享它(@IBDesignable)好吗