前面我们讲解了UISegemtedControl分段式控件, 现在让我们来看看 iOS 另一个非常常用的控件, UINavigationController.
1.UINavigationController常用属性
var topViewController: UIViewController! { get }
var visibleViewController: UIViewController! { get }
var viewControllers: [AnyObject]!
var navigationBarHidden: Bool
var navigationBar: UINavigationBar { get }
var toolbarHidden: Bool
var toolbar: UIToolbar! { get }
var delegate: UINavigationControllerDelegate?
var interactivePopGestureRecognizer: UIGestureRecognizer! { get }
var hidesBarsWhenKeyboardAppears: Bool
var hidesBarsOnSwipe: Bool
var barHideOnSwipeGestureRecognizer: UIPanGestureRecognizer { get }
var hidesBarsWhenVerticallyCompact: Bool
var hidesBarsOnTap: Bool
var barHideOnTapGestureRecognizer: UITapGestureRecognizer { get }
2.UINavigationController常用的方法
func pushViewController(viewController: UIViewController, animated: Bool)
func popViewControllerAnimated(animated: Bool) -> UIViewController?
func popToViewController(viewController: UIViewController, animated: Bool) -> [AnyObject]?
func popToRootViewControllerAnimated(animated: Bool) -> [AnyObject]?
func setViewControllers(viewControllers: [AnyObject]!, animated: Bool)
func setNavigationBarHidden(hidden: Bool, animated: Bool)
func setToolbarHidden(hidden: Bool, animated: Bool)
func showViewController(vc: UIViewController, sender: AnyObject!)
3.UINavigationController代理方法
optional func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool)
optional func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool)
4.代码演示
首先我们要再AppDelegate.swift文件中实现
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.grayColor()
self.window!.makeKeyAndVisible()
let viewController = ViewController()
let navigationController = UINavigationController(rootViewController: viewController)
self.window!.rootViewController = navigationController
return true
}
遵守代理协议
class ViewController: UIViewController, UINavigationControllerDelegate { }
自定义UINavigationController
func myNavigationContronller() {
self.title = "UINavigationContronller"
let backBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
let nextBarButtonItem = UIBarButtonItem(title: "下一页", style: UIBarButtonItemStyle.Plain, target: self, action: "nextAction")
self.navigationItem.leftBarButtonItem = backBarButtonItem
self.navigationItem.rightBarButtonItem = nextBarButtonItem
let topView = self.navigationController?.topViewController
println(topView)
let visibleView = self.navigationController?.visibleViewController
println(visibleView)
self.navigationController?.viewControllers
self.navigationController?.navigationBarHidden = false
let navigationBar = self.navigationController?.navigationBar
println(navigationBar)
self.navigationController?.toolbarHidden = false
let toolbar = self.navigationController?.toolbar
println(toolbar)
self.navigationController?.delegate = self
let pop = self.navigationController?.interactivePopGestureRecognizer
println(pop)
self.navigationController!.hidesBarsWhenKeyboardAppears = true
self.navigationController?.hidesBarsOnSwipe = true
let barHide = self.navigationController!.barHideOnSwipeGestureRecognizer
println(barHide)
self.navigationController!.hidesBarsWhenVerticallyCompact = true
self.navigationController?.hidesBarsOnTap = true
let barHideOnTap = self.navigationController!.barHideOnTapGestureRecognizer
println(barHideOnTap)
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
}
自定义代理方法以及监听方法
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
println("UINavigationController 将要显示")
}
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
println("UINavigationController 完全显示")
}
func backAction() {
println("点击了返回")
}
func nextAction() {
println("点击了下一页")
}
5.最终效果
PS: UINavigationController 是继承与 UIViewController 的, 所以里面的方法以及属性都是可以使用的.
好了, 这次我们就讲到这里, 下次我们继续~~
请发表评论