0

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]

    }
abdi musse
  • 17
  • 3
  • If your page view controller's frame is the entire bounds, why do you even need this other View Controller in the first place? Your hierarchy looks like Unnecessary VC --> PageVC --> NavController --> VC1. Why do you need the unnecessary VC? – 7to4 Apr 30 '17 at 21:45
  • Good question. My ideal approach would have been to just subclass pageVC rather than to create it in another VC however I couldnt figure how to set the transition style of the pageVC if subclassed. This line `let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)` accomplishes that if I create a PageVC within another VC. hope that makes sense. But I understand how its completely unnecessary. Nevertheless, I thought that was the issue but the same problem still exists – abdi musse Apr 30 '17 at 22:05
  • I'm sorry for taking the tangential path, but I think I can avoid this by removing the unnecessary VC. Is this unnecessary VC your app's initial screen? Do you use storyboards or code? – 7to4 Apr 30 '17 at 22:11
  • I guess I am not understanding where you are finding this unnecessary VC? The pageVC is the root VC set in the appdelegate. I have updated the question to include the full app delegate and pageVC code. I created all viewcontrollers in code. The hierarchy should be appdelegate -> pageVC -> VC1 – abdi musse Apr 30 '17 at 22:22
  • Thanks for the edit, I do not see the line addChildViewController() now. Can you point where you were initially adding it? – 7to4 Apr 30 '17 at 22:29
  • I only needed to add the line addChildViewController(pageVC) if the PageVC was being created in another VC. Because the updated code shows that pageVC is being subclassed, I do not need to add that line. Thanks for your patience. – abdi musse Apr 30 '17 at 22:42
  • Ohkay.. What I was referring to as the unnecessary VC, is what you mentioned as "created in another VC".. Do you still have any other probs? – 7to4 Apr 30 '17 at 22:50
  • Ok. yeah I took your advice and removed the unnecessary VC and just subclassed the pageViewController. Still having the issue of nav items in wrong position though. – abdi musse Apr 30 '17 at 22:56
  • Can you post code on where you add the UIBarButtonItem? – 7to4 Apr 30 '17 at 22:59
  • 1
    Ok. I think I seemed to have solved it. To troubleshoot further, I created an empty project with just the pageViewController, VC1. Everything worked. The reason why it worked is because this empty project has the Main Storyboard. As I said, I was making all of my ViewControllers without storyboards. I simply added a storyboard into my project and everything was working. I am still not sure why this was the issue. The app delegate code with the storyboard changed to let PageVC = PageViewController() window!.rootViewController = PageVC which is different from what I posted. – abdi musse Apr 30 '17 at 23:16
  • its weird how the adding the storyboard file was the fix even though I am not even using it. Maybe be I was setting up the window wrong? – abdi musse Apr 30 '17 at 23:19
  • If you don't mind, can you check by setting the frame of the PageViewController in your previous code? – 7to4 Apr 30 '17 at 23:19
  • I am not sure how or if I can even set the frame of the PageViewController. Its the root viewcontroller so the frame is automatically set to the frame of the window. I checked by print(view.bounds.size) and it shows the default screen size. – abdi musse Apr 30 '17 at 23:25
  • Ohkay.. It does seem out of my knowledge then. This is supposed to work, the way I see it. But, I may be missing something. – 7to4 Apr 30 '17 at 23:27
  • No worries. I appreciate your help. – abdi musse Apr 30 '17 at 23:33

1 Answers1

0

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

Community
  • 1
  • 1
7to4
  • 198
  • 1
  • 8