UIpageControl 指示点

Posted

技术标签:

【中文标题】UIpageControl 指示点【英文标题】:UIpageControl Indicator Dots 【发布时间】:2017-02-18 06:20:22 【问题描述】:

我有一个页面控制器,其指标 Dots Disappeared!我想在最后一页消失它们! 我也有另一个问题 我想将这些点从底部移动+100! 还有我的最后一个问题:哪种方法可以帮助我在页面视图控制器的任何页面中编写任何方法? (当用户看到这些页面时,我想控制所有页面)

这是我的代码

class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate 



let pageVC = UIPageControl()
lazy var VCArr : [UIViewController] = 

    return [self.VCInstance(name : "FirtsVC"),
            self.VCInstance(name : "SecondVC"),
            self.VCInstance(name :"ThirdVC"),
            self.VCInstance(name :"FourthVC"),
            self.VCInstance(name :"FivethVC")]

()

private func VCInstance(name : String) -> UIViewController 
    return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)




override func viewDidLoad() 
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self




    if VCArr.first != nil 

        print("first Page Reached!")



    

    if let firstVC = VCArr.first 
        setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)

    




override func viewDidLayoutSubviews() 
    super.viewDidLayoutSubviews()
    let pageVC = UIPageControl()


    for view in self.view.subviews 
        if view is UIScrollView 
            let pageControl = UIPageControl()
            pageControl.pageIndicatorTintColor = UIColor.gray
            pageControl.currentPageIndicatorTintColor = UIColor.white
            pageControl.backgroundColor = UIColor.darkGray
            pageControl.numberOfPages = VCArr.count
            pageControl.center = self.view.center
            self.view.addSubview(pageControl)
            pageControl.layer.position.y = self.view.frame.height - 100;
        else if view is UIPageControl
            view.backgroundColor = UIColor.clear
            pageVC.numberOfPages = 5
            pageVC.center = self.view.center
            pageVC.layer.position.y = self.view.frame.height - 180 ;

        
    




public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    guard let viewControllerIndex = VCArr.index(of: viewController) else 
        return nil
    

    let previousIndex = viewControllerIndex-1
    guard previousIndex >= 0  else 
        return nil
    

    guard VCArr.count > previousIndex else 
        return nil
    


    return VCArr[previousIndex]





public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    guard let viewControllerIndex = VCArr.index(of: viewController) else 
        return nil
    

    let nextIndex = viewControllerIndex+1
    guard nextIndex < VCArr.count
        else 
            return nil
    

    guard VCArr.count > nextIndex else 
        return nil
    
    if nextIndex == VCArr.count
        pageVC.isHidden = false

    



    return VCArr[nextIndex]





 public func presentationCount(for pageViewController: UIPageViewController) -> Int
    return VCArr.count




 public func presentationIndex(for pageViewController: UIPageViewController) -> Int
    guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else 

        return 0
    
        let firstIndex = firstViewControllerIndex - 1
        guard firstIndex >= VCArr.count  else 
            return VCArr.count



    

    return firstViewControllerIndex

public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) 
    if (VCArr.last!.isViewLoaded)
    
        print("It is Done!!!")

    


    

【问题讨论】:

【参考方案1】:

这就是答案

class pageVC : UIPageViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate 



let pageVC = UIPageControl()
lazy var VCArr : [UIViewController] = 

    return [self.VCInstance(name : "FirtsVC"),
            self.VCInstance(name : "SecondVC"),
            self.VCInstance(name :"ThirdVC"),
            self.VCInstance(name :"FourthVC"),
            self.VCInstance(name :"FivethVC")]

()

private func VCInstance(name : String) -> UIViewController 
    return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)




override func viewDidLoad() 
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self



    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
    if launchedBefore  
        print("Not first launch!")
        print("Should Launch MainTabBarController!")

        /////Not First Time launch! reach mainTabBarController//////////



     else 
        print("First launch, setting UserDefault.")
        UserDefaults.standard.set(true, forKey: "launchedBefore")
    
    //////Lock Rotation/////////
    var shouldAutorotate: Bool 
        return false
    
    var supportedInterfaceOrientations: UIInterfaceOrientationMask 
        return .portrait
    

    func viewWillAppear(_ animated: Bool) 
        super.viewWillAppear(animated)
        let value =  UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    
    func supportedInterfaceOrientations() -> UIInterfaceOrientationMask 
        return [UIInterfaceOrientationMask.portrait ]
    
    ///////Lock Roration//////
    if VCArr.first != nil 

        print("first Page Reached!")
    

    if let firstVC = VCArr.first 
        setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)

    




override func viewDidLayoutSubviews() 
    super.viewDidLayoutSubviews()

    let view = self.pageVC.currentPage
    for view in self.view.subviews 
        if view is UIScrollView 
            let pageVC = UIPageControl()
            self.pageVC.pageIndicatorTintColor = UIColor.lightText
            self.pageVC.currentPageIndicatorTintColor = UIColor.white
            self.pageVC.backgroundColor = UIColor.white
            self.pageVC.numberOfPages = VCArr.count
            self.pageVC.center = self.view.center
            self.view.addSubview(self.pageVC)
            pageVC.layer.position.y = self.view.frame.height - 150;
        else if view is UIPageControl
            view.backgroundColor = UIColor.clear
            pageVC.numberOfPages = 5
            pageVC.center = self.view.center
            pageVC.layer.position.y = self.view.frame.height - 100 ;

        
    






public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    guard let viewControllerIndex = VCArr.index(of: viewController) else 
        return nil



    




    let previousIndex = viewControllerIndex-1
    guard previousIndex >= 0  else 
        return nil
    

    guard VCArr.count > previousIndex else 
        return nil
    


    return VCArr[previousIndex]





public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    guard let viewControllerIndex = VCArr.index(of: viewController) else 
        return nil
    

    let nextIndex = viewControllerIndex+1
    guard nextIndex < VCArr.count
        else 
            return nil
    

    guard VCArr.count > nextIndex else 
        return nil
    




    return VCArr[nextIndex]





 public func presentationCount(for pageViewController: UIPageViewController) -> Int
    return VCArr.count




 public func presentationIndex(for pageViewController: UIPageViewController) -> Int
    guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else 


        return 0
    
        let firstIndex = firstViewControllerIndex - 1
        guard firstIndex >= VCArr.count  else 
            return VCArr.count



    

    return firstViewControllerIndex

public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) 
    self.pageVC.currentPage = pageViewController.viewControllers!.first!.view.tag

 if(viewControllers!.first?.view.tag == 4 ) 

    if (VCArr.last!.isViewLoaded)

    

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = mainStoryboard.instantiateViewController(withIdentifier: "FivethVC")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = viewController
        print("Last Page Reached!!!")

    

    







【讨论】:

以上是关于UIpageControl 指示点的主要内容,如果未能解决你的问题,请参考以下文章

UIpageControl 指示点

UIPageControl 指示点在页面更改时不会更改

如何以不同的方式设置 UIPageControl 的当前页面指示点的样式?

使用 UIPageControl 减小点大小或填充以适应屏幕上的所有页面指示器

在 UIPageViewController 的 UIPageControl 中增加指示器的大小

在 UIPageViewController 的 UIPageControl 中增加指示器的大小