當我需要自定義我的UIPageControl時,我使用this和this解決方案。帶點圖像的自定義UIPageControl
稍微修改它迅速的新版本中,我們有:
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()
}
}
我的問題是我無法改變的圖片,當你第一次啓動應用程序。 僅當頁面更改時纔會更改。
在viewDidLoad()
中,我加了setPage(0)
和updateDots()
,但沒有結果。我可能做錯了什麼?
下面是調用「updateDots()」的更好方法。還沒有嘗試過,但德米特里建議重寫numberOfPages和currentPage變量 - > http://stackoverflow.com/questions/12190147/customize-dot-with-image-of-uipagecontrol-at-index-0-of- uipagecontrol/41136917#41136917 – CodenameDuchess