定一個vc如果是UINavigationController,那可以使用pushViewController進行頁面跳轉,如果UINavigationBar,那當前vc使用present進行頁面跳轉(目前我所知道的是這樣的,有不同意見,請麻煩留言)。
1、把一個vc變成具有navi,可參考一下代碼進行設置:
let destination = SeconViewController()
destination.message = "傳遞的字符串"
// 創建navVC
let nav = UINavigationController(rootViewController: destination)
nav.navigationBar.backgroundColor = UIColor.cyan
self.present(nav, animated: true, completion: nil)
使用UINavigationController可以使導航欄風格保持一致。
2、自定義UINavigationBar,針對某個或者某些特殊導航條,可以使用UINavigationBar實現,具體代碼如下:
//聲明一個bar
var navigationBar:UINavigationBar?
//navigationBar初始化
navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: 64))
self.view .addSubview(navigationBar!)
//對navigationBar設置item和title
let navigationItem = UINavigationItem()
let leftBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onAdd))
let rightBtn = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(onRemove))
navigationItem.title = "第\(count)個導航欄"
navigationItem.setLeftBarButton(leftBtn, animated: true)
navigationItem.setRightBarButton(rightBtn, animated: true)
navigationBar?.pushItem(navigationItem, animated: true)
以上即可完成導航欄的設置。