背景上的图像pageViewController重叠相反
我有一个pageViewController
显示每个索引的背景图像。 它工作得很好,从左到右(向前)滑动,但是当您向后滑动时,背景图像相互重叠。 在实现背景图像之前,每个VC上的用户界面都可以正常工作,反转,所以我知道这是背景图像。 此外,如果我改变page curl
而不是在我的storyboard
scroll
,它反过来很好,只是不工作的scroll
。 这显然是一个已知的错误( 从UIPageViewController删除视图控制器 ),但是我需要在Swift中的解决scheme。
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) }
这个问题的答案非常简单,只需将clipsToBounds
上的clipsToBounds
设置为true
基于我在这里的回答,这里是一个Swift版本。 请注意,这是使用在线转换器从Objective-C转换到Swift(因为我不在我的开发机器 – 并且通常不在Swift中工作),有一些额外的摆弄,以获得弱的自我引用权利。 随意指出实施中的任何错误。
pageViewController.setViewControllers( viewControllers, direction: UIPageViewControllerNavigationDirectionForward, animated: true, completion: { [weak pageViewController] (finished: Bool) in if finished { dispatch_async(dispatch_get_main_queue(), { pageViewController.setViewControllers( viewControllers, direction: UIPageViewControllerNavigationDirectionForward, animated: false, completion: nil ) }) } })
[weak ...]
的窍门被称为“捕获列表”,在这个问题上 ,以及官方文档 “定义捕获列表”部分已经很好地说明了这一点 。
(如果你不跟踪这个目的,我们不希望我们的闭包有一个强大的参考pageViewController
,从而防止ARC不再需要它的释放,所以我们标记该variables作为一个弱引用,如果闭包是最后一件事情留给pageViewController
,sayonara)。
还有一点,我没有注意事物的可选方面,所以要注意的是实现者。
编辑:
看着你的代码,它看起来不像我的答案是你想要的。 我的答案地址是以编程方式设置视图控制器的问题,但你正在谈论刷卡。
我的猜测是,你没有实现所有的委托方法 – 我只看到viewControllerAtIndex
。 没有人必须做的viewControllerBeforeViewController
和viewControllerAfterViewController
的东西正常工作? (对不起,我处理这个类已经有一段时间了。)粗略的看一下这个教程让我觉得这样…
无论如何,蝙蝠错误地出现了。 请检查这个,也许详细说明你的问题,显示更多的代码等。
除了@GarySabo的回答,我不得不把图像的约束布局到超视图(在我的情况下默认视图),如下所示: