我已经实现了UIPageViewController的数据源方法,但是我的iOS应用程序底部仍然没有显示点。是否有任何解决方案可以使点出现在我的应用程序中?
我已经实现了UIPageViewController的数据源方法,但是我的iOS应用程序底部仍然没有显示点。是否有任何解决方案可以使点出现在我的应用程序中?
使用 UIPageViewController
时,默认情况下应该可见圆点。我猜你的背景是白色,而圆点也是白色,所以你看不到它们。
尝试更改圆点颜色:
Swift 4/5:
var appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
appearance.pageIndicatorTintColor = UIColor.red
appearance.currentPageIndicatorTintColor = UIColor.red
Swift 3:
->Swift 3:
var appearance = UIPageControl.appearanceWhenContainedIn(UIPageViewController.self, nil)
appearance.pageIndicatorTintColor = UIColor.red
appearance.currentPageIndicatorTintColor = UIColor.red
如果这并没有帮助,请确保您正在使用UIPageViewControllerTransitionStyleScroll
过渡样式。
同时,请确保从UIPageViewControllerDataSource
实现以下方法:presentationCount(for:)
和 presentationIndex(for:)
。
对于 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
}
UIPageViewController
的好教程。至少对我有效。
http://www.seemuapps.com/page-view-controller-tutorial-with-page-dots
这是与问题相关的部分:
确保您有适当的委托和数据源
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
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)!
}
使用presentationCountForPageViewController
和presentationIndexForPageViewController
数据源方法,然后显示UIPageViewController上的点。
Swift代码:
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
}
Objective-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;
}
这对我很有效,希望对你有所帮助
Swift 4 and Swift 5
private func changeIndicatorColor() {
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [YourUIPageViewController.self])
appearance.pageIndicatorTintColor = .lightGray
appearance.currentPageIndicatorTintColor = .black
}
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()
}
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
}
// 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.