一 、UINavigationController的介紹
UINavigationController管理堆棧視圖控制器和一個導航欄。它執行水平視圖轉換為推動的導航欄和彈出視圖,同時保持同步。
二 、 UINavigationController 的方法和屬性的介紹
1、 初始化
初始化
1、init()
2、init(coder: NSCoder) // 通過存儲的數據來創建
3、init(nibName: <#T##String?#>, bundle: <#T##Bundle?#>) // xib 的創建
4、 init(rootViewController: UIViewController) // 通過一個 UIViewController 來創建。
5、init(navigationBarClass: <#T##AnyClass?#>, toolbarClass: <#T##AnyClass?#>) // 通過一個類創建對象。
2 、導航控制控制器的跳轉
1> 添加導航控制器
導航控制器的創建
let RootNavigatonVc = UINavigationController.init(rootViewController: RootViewController.init())
RootNavigatonVc.delegate = self;
設置主窗口
window?.rootViewController = RootNavigatonVc
2>設置跳轉的按鈕
let NwBtn = UIButton.init(type: .custom)
NwBtn.frame = CGRect.init(x: 20, y: 120, width: UIScreen.main.bounds.width - 40, height: 60)
NwBtn.backgroundColor = UIColor.magenta
NwBtn.setTitle("導航跳轉", for: .normal)
NwBtn.setTitleColor(UIColor.white, for: .normal)
NwBtn.addTarget(self, action: #selector(pushToVc), for: .touchUpInside)
self.view.addSubview(NwBtn)
按鈕方法的實現(包含跳轉方法)
func pushToVc() -> Void {
控制頁面跳轉的方法
open func pushViewController(_ viewController: UIViewController, animated: Bool)
let pushVc = JumpViewController.init()
self.navigationController?.pushViewController(pushVc, animated: true)
}
3> 導航的返回方法
導航的返回上一個控制器
self.navigationController?.popViewController(animated: true)
導航返回根導航控制器
self.navigationController?.popToRootViewController(animated: true)
可以跳轉到堆棧內的任意一個控制器
let NavVcs = self.navigationController?.childViewControllers;
self.navigationController?.popToViewController((NavVcs?[1])!, animated: true)
3、 導航頭部和標題的修改和定義
設置導航的標題
self.navigationItem.title = "導航跳轉的標題"
修改導航標題的樣式
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red,NSFontAttributeName:UIFont.italicSystemFont(ofSize: 20)]
導航左邊按鈕
let leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "1"), style: .plain, target: self, action: #selector(leftBarButtonItemMethod(_:)))
設置按鈕
第一種:
self.navigationItem.setLeftBarButton(item: UIBarButtonItem?, animated: Bool)
第二種:
self.navigationItem.leftBarButtonItem = leftBarButtonItem
self.navigationItem.leftBarButtonItem = leftBarButtonItem
右邊按鈕
let rightBarButtonItem = UIBarButtonItem.init(title: "確定", style: .plain, target: self, action: #selector(rightBarButtonItemMethod(_:)))
self.navigationItem.rightBarButtonItem = rightBarButtonItem
修改按鈕文字的樣式
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.purple], for: .normal)
4、 獲取導航棧里的控制器& 當前控制器 & 獲取某個導航的頂部控制器
1> 所有的控制器的獲取
// TODO: 獲取當前導航棧里所有的控制器
let NavVcs = self.navigationController?.viewControllers
print(NavVcs!)
2> 當前的控制器
獲取當前顯示的控制器
let CurVc = self.navigationController?.visibleViewController
if CurVc==self {
print("相同")
}
3> 獲取某個導航的頂部控制器
獲取某個導航棧的頂部控制器
let TopVc = self.navigationController?.topViewController
print(TopVc!)
5、 導航控制器的隱藏和顯示
1> 基本的隱藏和顯示
導航的顯示和隱藏
self.navigationController?.isNavigationBarHidden = true
self.navigationController?.setNavigationBarHidden(false, animated: true)
2> 上下滑動控制器隱藏和顯示控制器
// TODO : 這個是當控制器上下滑動,導航的顯示與隱藏
self.navigationController?.hidesBarsOnSwipe = true
3> 用戶點擊控制器,導航才顯示與隱藏
// TODO : 這個是用戶點擊控制器,導航才顯示與隱藏
self.navigationController?.hidesBarsOnTap = true
4> 鍵盤的彈出會隱藏導航欄
// TODO : 這個是當鍵盤出現,導航會隱藏。
self.navigationController?.hidesBarsWhenKeyboardAppears = true
5> 當頁面垂直緊湊的時候,會隱藏導航欄
// TODO : 當導航欄的垂直size比較緊湊的時候,導航欄自動隱藏
self.navigationController?.hidesBarsWhenVerticallyCompact = true
6、 獲取上下滑動控制器隱藏導航欄的手勢& 點擊控制器,隱藏導航欄的手勢、
1> 上下隱藏導航的手勢
// TODO : 獲取上下滑動隱藏導航的手勢
let OnSwipeGesture = self.navigationController?.barHideOnSwipeGestureRecognizer
OnSwipeGesture?.maximumNumberOfTouches = 2
2> 點擊隱藏導航的手勢
// TDOD : 獲取點擊控制器隱藏導航的手勢
let OnTapGesture = self.navigationController?.barHideOnTapGestureRecognizer
OnTapGesture?.numberOfTouchesRequired = 2
7、隱藏導航的工具欄
控制導航是否顯示底部工具欄
self.navigationController?.isToolbarHidden = false
self.navigationController?.setToolbarHidden(true, animated: true)
8、設置是否滑動屏幕的左邊能夠返回
導航是否可以滑動屏幕左側返回
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
9 、UINavigationControllerDelegate 的代理
// 這是導航每次發生跳轉都調用該函數
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
// 當前頁控制器和跳轉后的控制器
print(navigationController,viewController)
}
// 導航每次發生跳轉前先調用的函數
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// 當前頁控制器和將要跳轉后的控制器
print(navigationController,viewController)
}
// 導航支持的屏幕方向
func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return .all
}
// 導航首先支持的屏幕方向
func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation {
return .portrait
}