在 UIPageViewController 屏幕底部添加点
Adding dots to bottom of UIPageViewController screen
我已经实现了 UIPageViewController 的数据源方法,但在我的 iOS 应用程序的底部仍然没有得到点。有人有任何解决方案可以使点出现在我的应用程序上吗?
当您使用 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
过渡样式。
此外,请确保从 UIPageViewControllerDataSource
实施此方法:presentationCount(for:)
和 presentationIndex(for:)
。
使用 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 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
}
在 Xcode 7 中使用基于页面的应用程序模板时,以下代码在插入到 ModelController class 时有效:
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()
}
如果有人仍在搜索此类问题,我找到了一个关于将页面点添加到 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)!
}
Swift 4 和 Swift 5
private func changeIndicatorColor() {
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [YourUIPageViewController.self])
appearance.pageIndicatorTintColor = .lightGray
appearance.currentPageIndicatorTintColor = .black
}
在 UiViewPageViewController class 你可以阅读:
// 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.
首先确保您的页面视图控制器设置正确,即:
- 导航设置为水平
- 过渡样式设置为滚动
设置完成后,转到您的 UIPageViewController class 并将这两个函数添加到:
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
}
除了故事板中的设置外,这两个功能需要才能出现圆点。
我已经实现了 UIPageViewController 的数据源方法,但在我的 iOS 应用程序的底部仍然没有得到点。有人有任何解决方案可以使点出现在我的应用程序上吗?
当您使用 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
过渡样式。
此外,请确保从 UIPageViewControllerDataSource
实施此方法:presentationCount(for:)
和 presentationIndex(for:)
。
使用 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 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
}
在 Xcode 7 中使用基于页面的应用程序模板时,以下代码在插入到 ModelController class 时有效:
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()
}
如果有人仍在搜索此类问题,我找到了一个关于将页面点添加到 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)!
}
Swift 4 和 Swift 5
private func changeIndicatorColor() {
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [YourUIPageViewController.self])
appearance.pageIndicatorTintColor = .lightGray
appearance.currentPageIndicatorTintColor = .black
}
在 UiViewPageViewController class 你可以阅读:
// 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.
首先确保您的页面视图控制器设置正确,即:
- 导航设置为水平
- 过渡样式设置为滚动
设置完成后,转到您的 UIPageViewController class 并将这两个函数添加到:
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
}
除了故事板中的设置外,这两个功能需要才能出现圆点。