在容器内移动页面控件

Posted

技术标签:

【中文标题】在容器内移动页面控件【英文标题】:Moving pagecontrol inside container 【发布时间】:2016-06-30 15:04:20 【问题描述】:

我目前有一个主视图控制器,其中有一个容器。这个容器包含一个 pageviewcontroller,它使用了 3 个不同的视图控制器。每个都有自己的颜色。绿色蓝色和粉红色。一切正常,但是我希望这些点出现在彩色框架内而不是在它之外。如何更改页面控件的位置?

  import Foundation
  import UIKit
   class TestPageController: UIPageViewController

private(set) lazy var orderedViewControllers: [UIViewController] = 
    return [self.newColoredViewController(color: "Green"),
            self.newColoredViewController(color: "Blue"),
            self.newColoredViewController(color: "Pink")]
()

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


override func viewDidLoad() 
    super.viewDidLoad()
    dataSource = self
    configPageControl()
    if let firstViewController = orderedViewControllers.first 
        setViewControllers([firstViewController],
                           direction: .forward,
                           animated: true,
                           completion: nil)
    

func configPageControl()
 var pageController = UIPageControl.appearance()
    pageController.backgroundColor = UIColor.clear()
    pageController.pageIndicatorTintColor = UIColor.black()
    pageController.currentPageIndicatorTintColor = UIColor.blue()
 

  




extension TestPageController: UIPageViewControllerDataSource


    func presentationCount(for pageViewController: UIPageViewController) -> Int 
        return orderedViewControllers.count
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int 
        return 0
    

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

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else 
            return nil
        

        guard orderedViewControllers.count > previousIndex else 
            return nil
        

        return orderedViewControllers[previousIndex]
    

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

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count

        guard orderedViewControllersCount != nextIndex else 
            return nil
        

        guard orderedViewControllersCount > nextIndex else 
            return nil
        

        return orderedViewControllers[nextIndex]
    


【问题讨论】:

【参考方案1】:

为此,您需要使用 UIPageControl(并放置在您想要的位置并设置 numberOfPages 属性),然后使用 UIPageControllerDelegate 的委托方法:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) 
    if finished && completed
        if let actualController = pageViewController.viewControllers?.first as? MyViewController
            self.pageControl.currentPage = actualController.index
        
    

【讨论】:

以上是关于在容器内移动页面控件的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中点击 Pagecontrol 时更改页面

以编程方式创建页面控件不显示

当我最小化页面时,网格内的网格会移动

在图像幻灯片和页面控件中添加 NSTimer 以自动移动图像

从常规页面移动到 asp 页面到用户控件后,脚本停止工作

如何在 iOS 中使用 UIImageview 添加 UIScrollview 的页面控件