UIPageControl视图子视图中的nil

时间:2015-12-24 10:00:58

标签: ios swift null uipagecontrol

我是ios swift的新手,想要通过图片更改UIPageControl点大小。 我在这里发现了同样的问题(UIPageControl custom class - found nil changing image to the dots) 但似乎答案是错误的,仍然在页面控制视图子视图(UIImageView)是零

由于我没有足够的声誉,不得不在这里问。

这里是我如何将图片视图添加到滚动视图

let imgOne = UIImageView(frame: CGRectMake(0, 0,scrollViewWidth, scrollViewHeight))
imgOne.image = UIImage(named: "1")
let imgTwo = UIImageView(frame: CGRectMake(scrollViewWidth, 0,scrollViewWidth, scrollViewHeight))
imgTwo.image = UIImage(named: "2")
let imgThree = UIImageView(frame: CGRectMake(scrollViewWidth*2, 0,scrollViewWidth, scrollViewHeight))
imgThree.image = UIImage(named: "3")
let imgFour = UIImageView(frame: CGRectMake(scrollViewWidth*3, 0,scrollViewWidth, scrollViewHeight))
imgFour.image = UIImage(named: "4")

self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(imgThree)
self.scrollView.addSubview(imgFour)

并且低于问题的答案

class PageControl: UIPageControl {
    var activeImage: UIImage!
    var inactiveImage: UIImage!
    override var currentPage: Int {
        willSet {
            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 // nil
            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)
    }

}

1 个答案:

答案 0 :(得分:0)

花了几天我用以下方式解决了...... 不知道解决方案有多好,随时可以改进...... 有2个问题

1:在函数updateDots中,应该看起来像这样

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

        if (imageView == nil) {
            imageView = UIImageView(frame: CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height));
            imageView!.image = self.currentPage == i ? activeImage : inactiveImage
            view.addSubview(imageView!)
        }
    }
}

2:调用此函数时,应该在(didSet)之前滚动(willSet)

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