问题描述
当我需要自定义UIPageControl时,我使用了此和解决方案.
When I needed to custom my UIPageControl I used this and this solution.
为新版本的swift稍作修改:
Slightly modifying it for the new version of swift we have :
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()
}
}
我的问题是,当您第一次启动应用程序时,我无法更改图片.仅在页面更改时更改.
My problem is that I can not change the picture when you first start app.It only change when the page is changed.
在viewDidLoad()
中,我添加了setPage(0)
和updateDots()
,但是没有结果.我可能在做错什么?
In viewDidLoad()
, I added setPage(0)
and updateDots()
, but there is no result. What could i be doing wrong?
推荐答案
Swift 3.0 ...您知道是否可以接受声明的风险:修改现有控件的子视图很脆弱".
Swift 3.0 ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
您将必须在viewDidAppear()中调用"updateDots()",并为页面控件调用valueChanged处理程序.
You will have to call "updateDots()" in viewDidAppear() and your valueChanged handler for the page control.
import UIKit
class CustomImagePageControl: UIPageControl {
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
override func awakeFromNib() {
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
}
func updateDots() {
var i = 0
for view in self.subviews {
if let imageView = self.imageForSubview(view) {
if i == self.currentPage {
imageView.image = self.activeImage
} else {
imageView.image = self.inactiveImage
}
i = i + 1
} else {
var dotImage = self.inactiveImage
if i == self.currentPage {
dotImage = self.activeImage
}
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
}
}
}
fileprivate func imageForSubview(_ view:UIView) -> UIImageView? {
var dot:UIImageView?
if let dotImageView = view as? UIImageView {
dot = dotImageView
} else {
for foundView in view.subviews {
if let imageView = foundView as? UIImageView {
dot = imageView
break
}
}
}
return dot
}
}
这篇关于带有点图像的自定义UIPageControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!