Search code examples
iosswiftuinavigationcontrolleruipageviewcontroller

UINavigationbar Items out of position


I am having issues trying to figure why navigation bar items are out of place--squeezed to the ends.

see https://drive.google.com/file/d/0B8bNRC27tztoTkZwamVlSllMNjA/view There isn't anything online that address this. My view hierarchy is

pageviewcontroller -> navViewController -> VC1.

I am able to solve the problem but removing this line

addChildViewController(pageViewController)

in the pageviewcontroller class but I need that for the pageviewcontroller to operate properly. Any help on this is greatly appreciated.

App delegate:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {



        let PageVC = PageViewController()


        window = UIWindow(frame: UIScreen.main.bounds)
        window!.rootViewController = PageVC
        window!.makeKeyAndVisible()

        return true
    }

Full pageviewcontroller code:

  import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
        var viewControllerList = [UIViewController]()



    override func viewDidLoad() {
        super.viewDidLoad()

        let vc1 = IntroViewController()
        let vc2 = CamViewController()
        let navigationController = UINavigationController(rootViewController: vc1)
        let navigationController2 = UINavigationController(rootViewController: vc2)
        viewControllerList = [navigationController,navigationController2]
        self.dataSource = self
        self.delegate = self

        let firstViewController = viewControllerList.first
            self.setViewControllers([firstViewController!], direction: .forward, animated: true, completion: nil)

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
        let previousIndex = vcIndex - 1
        guard previousIndex >= 0 else {return nil}
        guard viewControllerList.count > previousIndex else {return nil}

        return viewControllerList[previousIndex]

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}

        let nextIndex = vcIndex + 1

        guard viewControllerList.count != nextIndex else { return nil}

        guard  viewControllerList.count > nextIndex else { return nil }

        return viewControllerList[nextIndex]

    }

Solution

  • Thanks for the edits to the question. If you still find the issue and don't want to add the UIBarButtonItem piece of code, follow the below answer, but instead of negative spacing, give a positive one: How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]

    This is more of a hack. I can give a clearer answer if you can post the piece of code requested :)

    Do give an upvote for that answer too :)