在UIPageViewController屏幕底部添加点

时间:2016-04-20 04:58:09

标签: ios swift uipageviewcontroller

我已经实现了UIPageViewController的数据源方法,并且仍然没有在我的iOS应用程序底部获得点。有人有任何解决方案可以在我的应用程序上显示点吗?

8 个答案:

答案 0 :(得分:22)

对于Swift 3.0,您需要:

private func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.gray
    appearance.currentPageIndicatorTintColor = UIColor.white
    appearance.backgroundColor = UIColor.darkGray
}

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    setupPageControl()
    return self.images.count
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return 0
}

答案 1 :(得分:20)

使用UIPageViewController时,默认情况下应显示这些点。我猜你有一个白色的背景,点也是白色的,所以你只是看不到它们。

尝试更改点颜色:

Swift 2.2

var appearance = UIPageControl.appearanceWhenContainedIn(UIPageViewController.self, nil)
appearance.pageIndicatorTintColor = UIColor.redColor()
appearance.currentPageIndicatorTintColor = UIColor.redColor()

Swift 3.0

var appearance = UIPageControl.appearanceWhenContainedIn(UIPageViewController.self, nil)
appearance.pageIndicatorTintColor = UIColor.red
appearance.currentPageIndicatorTintColor = UIColor.red

Swift 4.0

var appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
appearance.pageIndicatorTintColor = UIColor.red
appearance.currentPageIndicatorTintColor = UIColor.red

如果没有帮助,请确保您使用UIPageViewControllerTransitionStyleScroll过渡方式。

还要确保从UIPageViewControllerDataSourcepresentationCount(for:)presentationIndex(for:)实施委托。

答案 2 :(得分:10)

如果有人仍在搜索此类问题,我找到了一个关于将页面点添加到UIPageViewController的好教程。它至少对我有用。

http://www.seemuapps.com/page-view-controller-tutorial-with-page-dots

这是这个问题的相关部分:

确保您拥有适当的委托和数据源

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource

要添加页面点指示,请按如下方式添加pageControl:

创建UIPageControl

的实例
var pageControl = UIPageControl()

现在添加以下功能。这将页面控件定位在屏幕的底部。当前页面指示将为黑色,其余指示将为白色。您可以根据应用程序的设计更改这些内容。

func configurePageControl() {
    pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
    self.pageControl.numberOfPages = orderedViewControllers.count
    self.pageControl.currentPage = 0
    self.pageControl.alpha = 0.5
    self.pageControl.tintColor = UIColor.black
    self.pageControl.pageIndicatorTintColor = UIColor.white
    self.pageControl.currentPageIndicatorTintColor = UIColor.black
    self.view.addSubview(pageControl)
}

现在在viewDidLoad()添加以下两行:

self.delegate = self
configurePageControl()

并添加以下功能,这将确保页面控制指示符在您滚动时更改为正确的页面。

// MARK: Delegate functions
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    let pageContentViewController = pageViewController.viewControllers![0]
    self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
}

答案 3 :(得分:5)

使用presentationCountForPageViewControllerpresentationIndexForPageViewController数据源方法然后显示UIPageViewController点,

swift code:

private func setupPageControl() {
        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.grayColor()
        appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
        appearance.backgroundColor = UIColor.darkGrayColor()
    }

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
  {
    setupPageControl()
    return self.arrimg.count
  }

  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
  {
    return 0
  }

目标-C代码:

- (void) setupPageControl
{
    [[UIPageControl appearance] setPageIndicatorTintColor: [UIColor lightGrayColor]];
    [[UIPageControl appearance] setCurrentPageIndicatorTintColor: [UIColor blackColor]];
    [[UIPageControl appearance] setTintColor: [UIColor blackColor]];

}

- (NSInteger) presentationCountForPageViewController: (UIPageViewController *) pageViewController
{
    [self setupPageControl];
    return [arrimg count];
}

- (NSInteger) presentationIndexForPageViewController: (UIPageViewController *) pageViewController
{
    return 0;
}

它为我工作,希望它有用

答案 4 :(得分:5)

Swift 4和Swift 4.1

private func changeIndicatorColor() {
    let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [YourUIPageViewController.self])
    appearance.pageIndicatorTintColor = .lightGray
    appearance.currentPageIndicatorTintColor = .black
}

答案 5 :(得分:4)

在Xcode 7中使用基于页面的应用程序模板时,以下代码在插入ModelController类时有效:

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    setupPageControl()
    return self.pageData.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}

private func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.darkGrayColor()
}

答案 6 :(得分:1)

首先确保正确设置了Page View Controller设置,即:

  1. 导航设置为水平
  2. 转换样式设置为滚动

Ensure Navigation is set to horizontal and transition style is set to scroll

设置完成后,转到您的UIPageViewController类,并在以下两个函数中添加这些函数:

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return //NUMBER OF DOTS HERE
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return //CURRENT DOT TO BE SELECTED
}

这是我的情况的一个例子:

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return viewControllerList.count
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return currentIndex
}

需要情节提要中的两个功能才能显示圆点。

答案 7 :(得分:0)

UiViewPageViewController 类中,您可以阅读:

// A page indicator will be visible if both methods are implemented, transition style is 'UIPageViewControllerTransitionStyleScroll', and navigation orientation is 'UIPageViewControllerNavigationOrientationHorizontal'.
// Both methods are called in response to a 'setViewControllers:...' call, but the presentation index is updated automatically in the case of gesture-driven navigation.
@available(iOS 6.0, *)
optional public func presentationCount(for pageViewController: UIPageViewController) -> Int // The number of items reflected in the page indicator.

@available(iOS 6.0, *)
optional public func presentationIndex(for pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.