override func viewDidLoad() {
super.viewDidLoad()
slider.delegate = self
slideControl.currentPageIndicatorTintColor = .clear
slideControl.pageIndicatorTintColor = .clear
slideControl.preferredIndicatorImage = UIImage(named: "pageControlImage")
slideControl.setIndicatorImage(UIImage(named: "activePageImage"), forPage: 0)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = slider.contentOffset.x / scrollView.frame.size.width
//slideControl.currentPage = Int(page)
slideControl.setIndicatorImage(UIImage(named: "activePageImage"), forPage: Int(page))
}
Post
Replies
Boosts
Views
Activity
override func viewDidLoad() { super.viewDidLoad() slider.delegate = self slideControl.currentPageIndicatorTintColor = .clear slideControl.pageIndicatorTintColor = .clear slideControl.preferredIndicatorImage = UIImage(named: "pageControlImage") slideControl.setIndicatorImage(UIImage(named: "activePageImage"), forPage: 0) } func scrollViewDidScroll(_ scrollView: UIScrollView) { let page = slider.contentOffset.x / scrollView.frame.size.width //slideControl.currentPage = Int(page) slideControl.setIndicatorImage(UIImage(named: "activePageImage"), forPage: Int(page)) }
This is how I have set my page control now. But the images are not appearing.
Primary-Color is one among the custom colours which I have created for my project. And I tried using in-built UIColors, it is just applying background for the whole page control.
Here is the code from the swift file. I call it from the viewDidLoad function. I tried calling it from the viewDidAppear function also but the result is the same.
import UIKit
extension UIPageControl {
func customPageControl(dotFillColor:UIColor, dotBorderColor:UIColor, dotBorderWidth:CGFloat) {
for (pageIndex, dotView) in self.subviews.enumerated() {
if self.currentPage == pageIndex {
dotView.backgroundColor = dotFillColor
dotView.layer.cornerRadius = dotView.frame.size.height / 2
}else{
dotView.backgroundColor = .white
dotView.layer.cornerRadius = dotView.frame.size.height / 2
dotView.layer.borderColor = dotBorderColor.cgColor
dotView.layer.borderWidth = dotBorderWidth
}
}
}
}
class IntroSliderViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var slider: UIScrollView!
@IBOutlet weak var slideControl: UIPageControl!
let slide1 = [:]
let slide2 = [:]
let slide3 = [:]
var slides = [ DictionaryString,String ]()
override func viewDidLoad() {
super.viewDidLoad()
slides = [slide1,slide2,slide3]
slider.isPagingEnabled = true
slider.contentSize = CGSize(width: self.view.bounds.width * CGFloat(slides.count), height: self.view.bounds.height)
slider.showsHorizontalScrollIndicator = false
slider.delegate = self
slideControl.customPageControl(dotFillColor: UIColor(named: "Primary-Color")!, dotBorderColor: UIColor(named: "Primary-Color")!, dotBorderWidth: CGFloat(2))
loadSlides()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
slideControl.currentPage = Int(page)
}
func loadSlides() {
for (index,content) in slides.enumerated(){
if let slide = Bundle.main.loadNibNamed("sliderContent", owner: self, options: nil)?.first as? SliderContentView {
slide.backgroundImage.image = UIImage(named: content["background"]!)
slide.mainImage.image = UIImage(named: content["mainImage"]!)
slide.heading.text = content["heading"]
slide.textContent .text = content["textContent"]
slider.addSubview(slide)
slide.frame.size.width = self.view.bounds.size.width
slide.frame.size.height = self.view.bounds.size.height
slide.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}
}
}
}
Thank you for the help. It worked.
The ViewController is displayed using present function.
self.present(IntroSliderViewController(), animated: true, completion: nil)
And here is the error which occurs when I call this function:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file myPlan/IntroSliderViewController.swift, line 25
2021-05-05 16:18:30.705352+0530 myPlan[11130:802505] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file myPlan/IntroSliderViewController.swift, line 25
(lldb)