简体   繁体   中英

Is there a way to keep the navigation bar across multiple views?

So I have 2 views and when I click on "Go to second view", The navigation bar should be fixed and the only part that should be moving is the view below the Navigation Bar. Is there a possibility that swift allows this either with storyboards or programmatically?

Yes. There is.

1. Create PageViewController.swift and paste the code below there

//
//  Copyright © 2018 fewlinesofcode.com All rights reserved.
//
import UIKit

class PageViewController: UIPageViewController {
    fileprivate(set) var currentPageIndex: Int = 0
    
    var pages = [UIViewController]()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if let firstViewController = page(at: currentPageIndex) {
            setViewControllers([firstViewController], direction: .forward, animated: false, completion: { completed in })
        }
    }
    
    func resetIndex() { currentPageIndex = 0 }
    
    func page(at index: Int) -> UIViewController? {
        guard index >= 0 && index < pages.count else { return nil }
        return pages[index]
    }
}

extension PageViewController: UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let pageIndex = pages.index(of: viewController), currentPageIndex != 0 else {
            return nil
        }
        currentPageIndex = pageIndex
        currentPageIndex -= 1
        return page(at: currentPageIndex)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let pageIndex = pages.index(of: viewController) else {
            return nil
        }
        currentPageIndex = pageIndex + 1
        
        if currentPageIndex == pages.count {
            return nil
        }
        return page(at: currentPageIndex)
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return pages.count
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return currentPageIndex
    }
}

extension PageViewController {
    func showPrev(completion: ((Bool) -> Void)? = nil) {
        guard currentPageIndex > 0 else { return }
        currentPageIndex -= 1
        setViewControllers([pages[currentPageIndex]], direction: .reverse, animated: true, completion: completion)
    }
    
    func showNext(completion: ((Bool) -> Void)? = nil) {
        guard currentPageIndex < pages.count - 1 else { return }
        currentPageIndex += 1
        setViewControllers([pages[currentPageIndex]], direction: .forward, animated: true, completion: completion)
    }
}

2. Subclass it like in the sample below:

class ViewController: PageViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        setupChildViewControllers()
    }
    
    private func setupChildViewControllers() {
        let vc1 = <Instantiate your VC 1>
        let vc2 = <Instantiate your VC 2>
        
        pages = [
            vc1, vc2
        ]
    }
}

3. Setup navigation

Just embed ViewController in UINavigationController .

Each view controller has it's own navigation bar execution. The only way this would be possible is instead of moving to another view controller, you could create a subclass of UIView, and present a new UIView upon clicking that button by adding it to the subview of the base view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM