有没有办法使导航栏跨多个视图?

时间:2018-10-22 17:43:31

标签: ios swift xcode navigationbar

所以我有2个视图,当我单击“转到第二个视图”时,导航栏应固定,唯一要移动的部分是导航栏下方的视图。迅速有可能通过情节提要或以编程方式允许这样做吗?

2 个答案:

答案 0 :(得分:2)

是的。有。

1。创建PageViewController.swift并粘贴下面的代码

//
//  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。像下面的示例一样将其子类化:

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。设置导航

只需将ViewController嵌入UINavigationController

答案 1 :(得分:0)

每个视图控制器都有自己的导航栏执行。唯一可行的方法是代替移动到另一个视图控制器,而可以创建UIView的子类,并在通过单击该按钮并将其添加到基本视图控制器的子视图中来呈现新的UIView。