Background Images on the ViewController page overlap in reverse order

I have a pageViewController that shows a background image for an index. It works great from left to right (forward), but when scrolling in the opposite direction, the background images overlap. Before I executed the background image, the user interface on each VC worked fine and vice versa, so I know its 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 seems to be a known bug ( Removing the view controller from the UIPageViewController ), however I need a solution in 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) } 
+3
ios swift uipageviewcontroller
Nov 16 '15 at 18:55
source share
3 answers

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

+12
Dec 04 '15 at 15:08
source share

Based on my answer here , here is the Swift version. Please note that this was converted from Objective-C to Swift using an online converter (since I am not on my development computer and usually do not work in Swift), with some added skills to get the right weak link. Feel free to point out any errors in the implementation.

 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 ) }) } }) 

The trick with [weak ...] is called the "Capture List" and is well covered in this matter , as well as official documents , the section "Defining the Capture List".

(The intention, if you did not track this, is that we do not want our closure to have a strong link to the pageViewController and thus prevent ARC from freeing it if it is no longer needed. Therefore, we mark this variable as weak link, and if closing is the last thing left with a link to pageViewController , sayonara).

Another point: I did not pay attention to the aspect of options, so the developer is caveat.




EDIT:

Looking at your code, it doesn't seem like my answer is what you want. The problem is that the addresses of my answer programmatically configure the view controller, but you're talking about scrolling.

I assume that you did not implement all the delegate methods - I only see viewControllerAtIndex . You do not need to do viewControllerBeforeViewController and viewControllerAfterViewController for the item to work correctly? (Sorry, it's been a while since I was in this class.) A quick look at this tutorial makes me think so ...

In any case, he understands that the wrong tree is barking. Please check this and maybe clarify your question, show more code, etc.

+2
Nov 18 '15 at 17:22
source share

In addition to @GarySabo's answer, I had to map the image limitations to the superview (in my case, the default):

enter image description here

+1
Mar 25 '16 at 22:33
source share



All Articles