gpt4 book ai didi

ios - UIPageControl View subview 中为 nil

转载 作者:行者123 更新时间:2023-11-30 13:49:38 25 4
gpt4 key购买 nike

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

由于我没有足够的声誉,所以必须在这里提问。

这是我如何将 ImageView 添加到 ScrollView

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

}

最佳答案

花了几天时间,我通过以下方式解决了这个问题......不知道解决方案怎么样,欢迎改进...有 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()
}
}

关于ios - UIPageControl View subview 中为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com