【发布时间】:2015-11-16 18:55:02
【问题描述】:
我有一个pageViewController,它显示每个索引的背景图像。从左到右(向前)滑动效果很好,但是当您反向滑动时,背景图像会相互重叠。在我实现背景图像之前,每个 VC 上的 UI 都可以正常工作,所以我知道它是背景图像。此外,如果我在我的storyboard 中更改为page curl 而不是scroll,它反向工作正常,只是不适用于scroll。这显然是一个已知的错误 (Removing a view controller from UIPageViewController) 但是我需要 Swift 中的解决方案。
pageviewController:
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard
let currentPageViewController = viewController as? SinglePageViewController,
let currentIndex = indexOfForecast(currentPageViewController.forecast!) as? Int where currentIndex != 0 else { return nil } //fixed bug where index was -0 only on scroll
return viewControllerAtIndex(currentIndex - 1)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard
let lastForecast = forecasts?.last,
let currentPageViewController = viewController as? SinglePageViewController, currentForecast = currentPageViewController.forecast,
let currentIndex = indexOfForecast(currentForecast) as? Int where currentIndex != forecasts?.indexOf(lastForecast) else {return nil}
return viewControllerAtIndex(currentIndex + 1)
}
func viewControllerAtIndex(index: Int) -> UIViewController? {
if let forecast = forecasts?[index] {
let time = forecastTimes[index]
let imageURL = conditionsIcons[index]
let backgroundImageName = backgroundImageNames.names[index]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SinglePageViewController") as! SinglePageViewController
vc.forecast = forecast
vc.time = time
vc.imageURL = imageURL
vc.backgroundImageName = backgroundImageName
return vc
}
return nil
}
func showVC() {
if let firstVC = viewControllerAtIndex(0) {
busyAlertController.dismiss()
let viewControllers = [firstVC]
self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
}
singlePageViewController:
override func viewDidLoad() {
super.viewDidLoad()
backgroundImage = UIImageView(image: UIImage(named: backgroundImageName!))
backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill
self.view.insertSubview(backgroundImage, atIndex: 0)
}
【问题讨论】:
-
从我所看到的,看起来你正在添加多个视图控制器,但永远不要删除它们,也许这是从左到右故意的,但从右到左你不想再次添加
-
谢谢@Knight0fDragon 你的评论引导我搜索,我在这里找到了这个确切的问题:stackoverflow.com/questions/14220289/… 但是我留下了我的问题,因为答案只在 Obj-C 中,我正在使用 Swift .
标签: ios swift uipageviewcontroller