将点添加到UIPageViewController屏幕的底部

问题描述 投票:18回答:8

我已经实现了UIPageViewController的数据源方法,但是在我的iOS应用程序的底部仍然没有点。有人有解决办法让点出现在我的应用程序上吗?

ios swift uipageviewcontroller
8个回答
23
投票

当您使用UIPageViewController时,默认情况下,点应可见。我想您的背景是白色,点也是白色的,所以您根本看不到它们。

尝试更改点的颜色:

Swift 4/5

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

Swift 3

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

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

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


23
投票

对于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
}

12
投票

[如果有人仍然在搜索这种问题,我找到了一个很好的教程,关于将页面点添加到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)!
}

6
投票

使用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;
}

它为我工作,希望对您有所帮助


6
投票

Swift 4和Swift 5

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

5
投票

[在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()
}

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
}

在情节提要中的设置之外的两个功能都是必需,以便出现点。


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.
© www.soinside.com 2019 - 2024. All rights reserved.