iOS隐藏某个页面导航栏的方法

引言

在实际开发过程中,我们有时需要隐藏某个页面的导航栏,同时希望它push新页面时,新页面的导航栏仍然存在,并且不影响右侧滑返回上级页面功能,怎么实现呢?

方法多种多样,以下介绍一种比较简单的设置方法:

效果图如下:

代码设置如下:

//
//  HistoryRecordViewController.swift
//  HiddenNavigationBarDemo
//
//  Created by Kevin on 2018/7/22.
//  Copyright © 2018年 Kevin. All rights reserved.
//

import UIKit

///MARK: -历史记录页面
class HistoryRecordViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        hideNavigationBar()
    }
    
    private func hideNavigationBar() {
        /**
         *注意,要实现UINavigationControllerDelegate代理
         */
        self.navigationController?.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


// MARK: - UINavigationControllerDelegate
extension HistoryRecordViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        navigationController.setNavigationBarHidden(viewController.isKind(of: HistoryRecordViewController.classForCoder()), animated: animated)
    }
}
复制代码

以上就是全部设置代码了,是不是很简单呢。

github地址

你可能感兴趣的:(iOS隐藏某个页面导航栏的方法)