Swift/iOS8:为什么没有显示页面控制指示符?
我正在实现一个简单的图库视图控制器,其中应用程序显示用户可以滚动浏览的全范围图像的小范围。我使用UIPageViewController,我认为,如果我实现了正确的数据源功能,应该自动显示页面控制指示器。但是我仍然看不到任何指标。Swift/iOS8:为什么没有显示页面控制指示符?
在我的主要GalleryViewController
:
class GalleryViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pageController: UIPageViewController?
var pageContent = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
.... some code to load images into the pageContent array ....
pageController = UIPageViewController(transitionStyle:.Scroll, navigationOrientation:.Horizontal,options:nil)
pageController?.delegate = self
pageController?.dataSource = self
... some more standard code to add the page controller to self.view etc.
var pageControl = UIPageControl.appearance()
pageControl.pageIndicatorTintColor = UIColor.lightGray()
pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
pageControl.backgroundColor = UIColor.blackColor()
pageController!.didMoveToParentViewController(self)
}
func viewControllerAtIndex(index:Int) -> ContentViewController? {
....
}
我已经实现了两个强制性协议方法viewControllerBeforeViewController和viewControllerAfterController以及两个必要的那些寻呼,presentationCountForPagesViewController和presentationIndexForPageViewController。
我的ContentViewController
有一个UIImageView
填充整个屏幕。不过,即使当我从屏幕底部减小尺寸时,我也看不到任何页面指示灯。 我在这里错过了什么吗?如果我没有提供足够的信息,请告诉我。我一直在尝试重写一个我在5年前编写的Objective-C中的旧应用程序 - 完全在Swift for iOS8中,Xcode等发生了很大变化,令人困惑。 谢谢!
从here摘自:
为了显示UIPageControl,你需要实现两个可选的数据源的方法。只是返回的为presentationCountForPageViewController页的整数和最初选择的指数presentationIndexForPageViewController
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pageContent.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
为什么不使用迅速3+ 版本的泽尔B.答案
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return self.providerItemList?.providerItems?.count ?? 0
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
这是为我做的。旧的方法名称(例如:'func presentationCountForPageViewController')在Xcode中未被标记,并且仍然完全有效。切换到新的方法名现在显示页面控制指示器。 – Alexander
同样适合我! – philthomas26
即使您将更改UIPageControl的颜色,它也可能不会显示。如果UIPageController在UITabBarController中,则TabBar将覆盖UIPageControl。 找到它的最好方法是使用选项Debug-> View Debubging-> CaptureViewHierarchy。然后在左下角的过滤器字段中键入“pageControl”。如果您双击它,在导航应该定位在屏幕上(看看截图)
您可以更改的位置是这样的:
let controlHeight: CGFloat = 50
let bottomSpace: CGFloat = 20
let yPosition = view.frame.size.height - (controlHeight + bottomSpace)
let fullScreenWidth = view.frame.size.width
let frame = CGRect(x: 0, y: yPosition, width: fullScreenWidth, height: controlHeight)
pageControl.frame = frame
我的错误是在presentationCount()委托方法中,将计数值设置为比页面视图控制器中的实际内容页更多。
我已经完成了 - 仍然没有显示。 –
我把一个println语句调试到我的presentationCountForPageViewController方法中,它永远不会被调用。这可能是为什么我的页面计数器点没有显示 - 但为什么不被调用?是否使用UIPageViewController自动创建页面计数器? –
你永远不会添加pageController.view作为self.view的子视图。 – jrc