簡體   English   中英

有沒有辦法讓導航欄跨多個視圖?

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

所以我有 2 個視圖,當我點擊“轉到第二個視圖”時,導航欄應該是固定的,唯一應該移動的部分是導航欄下方的視圖。 swift 是否有可能通過情節提要或以編程方式允許此操作?

是的。 有。

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.設置導航

只需在UINavigationController嵌入ViewController

每個視圖控制器都有自己的導航欄執行。 唯一可行的方法是不要移動到另一個視圖控制器,您可以創建 UIView 的子類,並在單擊該按鈕時通過將其添加到基本視圖控制器的子視圖來呈現一個新的 UIView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM