我在UIPageViewController中有四个页面,我想隐藏最后一页上的点。我成功地创建了一个在UIPageViewController的最后一页调用的函数。基本上,如果当前视图是最后一个视图,则调用它。但是,我该用什么功能来暂时隐藏这些点?
我发现了这个https://stackoverflow.com/a/32016614/5700898,但它对该功能内部的内容没有帮助。正常方法(隐藏所有页面上的页面点)不是我需要的。
如何仅在UIPageViewController的最后一页上隐藏页面指示符点?
修改:这是我要求的代码。
import UIKit
class TutorialController: UIPageViewController {
let pageControl = UIPageControl.appearanceWhenContainedInInstancesOfClasses([])
var currentview = "0"
private func stylePageControl() {
pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.pageIndicatorTintColor = UIColor.darkGrayColor()
pageControl.backgroundColor = UIColor.whiteColor()
}
private func hidePageControl() { // this should let us hide the dots on the fourth view, by changing color.
pageControl.backgroundColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
pageControl.currentPageIndicatorTintColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
pageControl.pageIndicatorTintColor = UIColor(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1.0)
}
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newViewController("1"),
self.newViewController("2"),
self.newViewController("3"),
self.newViewController("4")]
}()
private func newViewController(number: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("Tutorial\(number)") // calls the next view controller.
}
override func viewDidLoad() {
super.viewDidLoad()
stylePageControl() // just changing the color, this works.
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
}
// MARK: UIPageViewControllerDataSource
extension TutorialController: UIPageViewControllerDataSource {
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
return 0
}
return firstViewControllerIndex
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
currentview = "\(nextIndex)"
print("now on tutorial slide \(currentview)")
if currentview == "4" { // this is called successfully when the fourth page comes into view.
print("we are now on the fourth slide; hiding the page dots...") // this successfully prints.
UIPageControl.appearanceWhenContainedInInstancesOfClasses([TutorialController.self]).hidden = true // this should hide the all page controller dots, but it just doesn't work.
pageControl.hidden = true // also should hide page controller dots, also doesn't work.
hidePageControl() // this should change the color of the page controller dots, but doesn't (note that this function works to change color when I call it on viewDidLoad, but that's not where I want it; it doesn't work here).
print(pageControl.hidden) // always prints false, even though I am trying to set it to true.
}
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
答案 0 :(得分:1)
使用UIPageViewControllerDelegate方法:
let pageControl = UIPageControl()
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (index == numberOfPages()-1){
pageControl.hidden = true
}else{
pageControl.hidden = false
}
}
答案 1 :(得分:1)
我发现你实际上无法隐藏页面控件,因为它永远不会起作用。但有点黑客就是将其添加到您的代码中:
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
if let lastVC = pendingViewControllers.last?.isKindOfClass(youLastViewController){
if lastVC == true {
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.mainScreen().bounds
} else if view is UIPageControl {
view.center.y = self.view.frame.height * 1.5
}
}
}else {
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.mainScreen().bounds
} else if view is UIPageControl {
view.center.y = self.view.frame.height * 0.92
}
}
}
}
}
}
这实际上会移动屏幕的页面控件,就像隐藏它一样。
答案 2 :(得分:1)
这里是UIPageViewController的完整代码,用于隐藏最后一页的点,它工作正常
首先为UIViewController创建类并添加UIPageViewControllerDelegate,UIPageViewControllerDataSource
为pageControl创建插座
@IBOutlet weak var pageControl:UIPageControl!
为uipagecontroller创建变量
- some_meth(blog( ENV[ "site" ] ).articles.select { | a | a.data[ :category ] == category }, 'all')
ViewDidLoad中的:添加以下行:
var pageControllerContainer : UIPageViewController!
var pages = [UIViewController]()
var currentIndex : Int?
private var pendingIndex : Int?
private var lastIndex : Int?
现在为pageviewcontroller创建以下委托方法:
let page1 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourFirstPageViewController") as! YourFirstPageViewController
let page2 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourSecondPageViewController") as! YourSecondPageViewController
let page3 : UIViewController! = self.storyboard?.instantiateViewController(withIdentifier: "YourThirdPageViewController") as! YourThirdPageViewController
pages.append(page1)
pages.append(page2)
pages.append(page3)
pageControllerContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageControllerContainer.delegate = self
pageControllerContainer.dataSource = self
pageControllerContainer.setViewControllers([page1], direction: .forward, animated: true, completion: nil)
view.addSubview(pageControllerContainer.view)
view.bringSubview(toFront: pageControl)
pageControl.numberOfPages = pages.count
pageControl.currentPage = 0
之后创建以下数据源方法
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)
if currentIndex == 0
{
return nil
}
let previousIndex = abs((currentIndex! - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)
if currentIndex == pages.count - 1
{
return nil
}
let nextIndex = abs((currentIndex! + 1 ) % pages.count)
return pages[nextIndex]
}
这将允许隐藏页面控制器,当涉及到最后一个索引意味着最后一页
希望这会有所帮助:-)谢谢你......
答案 3 :(得分:0)
在这里查看接受的答案以获取UIPageControl(带点的视图):
Access the UIPageControl created by iOS6 UIPageViewController?
然后,一旦你有了pageControl,你可以在pageViewController: didFinishAnimating:
中查看它是否是最后一页,如果是,请设置pageControl.hidden = true
。
答案 4 :(得分:-1)
override func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
if /*current index is the last one*/ {
return 0
} else {
/*return number of pages*/
}
}