如何在UIPageViewController

时间:2017-02-07 06:32:34

标签: ios swift

在我的UIPageViewController我想在从一个页面拖动到另一个页面时设置不同的背景颜色。所以我的默认颜色会在那里,但会在不同的索引转换上显示不同的颜色。在我UIPageViewControllerDelegate下的那一刻,我得到了当前页面的索引号:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        if let firstViewController = viewControllers?.first,
            let index = orderedViewControllers.index(of: firstViewController) {
            viewControllerDelegate?.pageViewController(pageViewController: self, didUpdatePageIndex: index)

            if index == 0 {

                (parent as? MainViewController)?.container.backgroundColor = UIColor.red
            }
            else if index == 1 {

                (parent as? MainViewController)?.container.backgroundColor = UIColor.black
            }

        }


    }

但是如何在它们之间设置颜色。

2 个答案:

答案 0 :(得分:0)

didFinishLaunchingWithOptions

中添加此内容
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor blueColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.backgroundColor = [UIColor blackColor];

如果要设置外观,请添加以下行:

UIPageControl *pageControl = [UIPageControl appearanceWhenContainedIn:[MyViewController class], nil];

答案 1 :(得分:0)

您在阵列中的颜色

  let Array: [(name: String, color: UIColor)] = [("Red", UIColor.red), ("Blue",     UIColor.blue),("Green", UIColor.green)]

  print( Array[index]) // index is Your array indexNumber. 

并以背景颜色传递此数组值

  (parent as? MainViewController)?.container.backgroundColor = [Pass this array index color]