Search code examples
iosuiimagefullscreenuipageviewcontrollertabview

UIPageViewController UIImage not showing full screen


I am making an application that is a tab-view controller. For the first tab, I have a UIPageViewController that is scrolling through pictures. Everything works properly, except for that the picture is not showing full screen. I have constraints set up for the image view to cover the whole view controller but when it gets loaded into the page view, it doesn't cover all the way to the bottom. The image gets cut off by the scrolling dot indicators and then it is just white.

I'm sure it is simple, but is there a way that the images will cover the full screen like I have the constraints set up to do?

As you can see, the image stops at the bottom where the dots begin. Where as I want it to go all the way down to the bottom. So the area with the dots is transparent.


Solution

  • There's quite a simple solution for this problem on that page: http://tunesoftware.com/?p=3363

    It is done by overriding the viewDidLayoutSubviews method of UIPageViewController and basically doing two things:

    1. Increasing the bounds of the UIScrollView (the content) inside the UIPageViewController by setting it's frame to the bounds of the page controller's view
    2. Moving the UIPageControl element (the dots) to the front of the view hierarchy so that it's in front of the content

    Here's the code he provides (in case the link gets broken):

    import UIKit
    
    class TSPageViewController: UIPageViewController {
        
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
    
            var subViews: NSArray = view.subviews
            var scrollView: UIScrollView? = nil
            var pageControl: UIPageControl? = nil
            
            for view in subViews {
                if view.isKindOfClass(UIScrollView) {
                    scrollView = view as? UIScrollView
                }
                else if view.isKindOfClass(UIPageControl) {
                    pageControl = view as? UIPageControl
                }
            }
            
            if (scrollView != nil && pageControl != nil) {
                scrollView?.frame = view.bounds
                view.bringSubviewToFront(pageControl!)
            }
        }
    }
    

    All you have to do after that, is:

    • If you set your page controller in the storyboard, then just go to the identity inspector pane and change its class to TSPageViewController
    • If you set your page controller in code, then just make sure the class you defined for it inherits the TSPageViewController class.