pageViewController上的背景图像反向重叠 [英] background Images on pageViewController overlap in reverse

查看:83
本文介绍了pageViewController上的背景图像反向重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pageViewController ,它显示了每个索引的背景图像。它可以从左向右(向前)轻扫,但是当您反向滑动时,背景图像会相互重叠。在我实现背景图像之前,每个VC上的UI工作正常和反向,所以我知道它是背景图像。此外,如果我更改为页面卷曲而不是滚动在我的故事板中它反向工作正常,只是不适用于滚动。这显然是一个已知的错误(从UIPageViewController中删除视图控制器)但是我在Swift中需要一个解决方案。

I have a pageViewController that shows a background image per the index. It works fine swiping left to right (forward), but when you swipe in reverse the background images overlap each other. Before I implemented the background image, the UI on each VC worked fine forward and reverse, so I know it's the background image. Also if I change to page curl instead of scroll in my storyboard it works fine in reverse, just doesn't work for scroll. This apparently is a known bug (Removing a view controller from UIPageViewController) however I need a solution in Swift.

pageviewController:

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:

singlePageViewController:

override func viewDidLoad() {
        super.viewDidLoad()

        backgroundImage = UIImageView(image: UIImage(named: backgroundImageName!))
        backgroundImage.contentMode = UIViewContentMode.ScaleAspectFill
        self.view.insertSubview(backgroundImage, atIndex: 0)
}


推荐答案

这个问题的答案很简单,只需将图片上的 clipsToBounds 设置为 true

The answer to this problem was so simple, just set clipsToBounds on the image to true

这篇关于pageViewController上的背景图像反向重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆