IOS 导航栏 UINavigationController 常用

1 创建:FirstViewController、SecondViewController
2、在FirstViewController的viewDidLoad设置属性
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = “第一页”
self.view.backgroundColor = UIColor.brown
self.navigationItem.rightBarButtonItem =
UIBarButtonItem(title:”下一页”, style:
UIBarButtonItemStyle.plain, target:self, action:

selector(FirstViewController.nextPage))

}
func nextPage()
{
let viewController = SecondViewController()
self.navigationController?.pushViewController(viewController,
animated:true)
}
3、AppDelegate.swift中的didFinishLaunchingWithOptions
func application(_ application:UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject:
AnyObject]?) -> Bool
{
// Override point for customization after application launch.
let viewController = FirstViewController()
let navigationController =
UINavigationController(rootViewController:viewController)
self.window?.rootViewController =navigationController
return true
}
4、SecondSubViewController.swift
import UIKit
var pageNum = 0
class SecondViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
pageNum = pageNum + 1
self.title = “第(pageNum)页”
self.view.backgroundColor = UIColor.purple
let push = UIButton(frame:CGRect(x:40, y:120,
width:240, height:40))
push.setTitle(“Push Page”, for:UIControlState())
push.backgroundColor = UIColor.orange
push.addTarget(self, action:

selector(SecondViewController.pushPage), for:

UIControlEvents.touchUpInside)
self.view.addSubview(push)
let pop = UIButton(frame:CGRect(x:40, y:180,
width:240, height:40))
pop.setTitle(“Pop Page”, for:UIControlState())
pop.backgroundColor = UIColor.orange
pop.addTarget(self, action:

selector(SecondViewController.popPage), for:

UIControlEvents.touchUpInside)
self.view.addSubview(pop)
let index = UIButton(frame:CGRect(x:40, y:280,width:240, height:40))
index.setTitle(“Goto Index Page”, for:
UIControlState())
index.backgroundColor = UIColor.orange
index.addTarget(self, action:

selector(SecondViewController.gotoIndexPage), for:

UIControlEvents.touchUpInside)
self.view.addSubview(index)
let root = UIButton(frame:CGRect(x:40, y:340,
width:240, height:40))
root.setTitle(“Goto Root Page”, for:UIControlState())
root.backgroundColor = UIColor.orange
root.addTarget(self, action:

selector(SecondViewController.gotoRootPage), for:

UIControlEvents.touchUpInside)
self.view.addSubview(root)
}
}
func pushPage()
{
let viewController = SecondViewController()
self.navigationController?.pushViewController(viewController,animated:true)
}
func popPage()
{
self.navigationController?.popViewController(animated:true)
}
func gotoIndexPage()
{
let viewController =
self.navigationController?.viewControllers[2]
self.navigationController?.popToViewController(viewController!,
animated:true)
}
func gotoRootPage()
{
self.navigationController?.popToRootViewControllerAnimated(animated:true)
}

6、更改导航栏的可见性/导航栏样式修改:FirstSubViewController.swift中的viewWillAppear
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
self.navigationController?.setToolbarHidden(false,animated:false)
self.navigationController?.setNavigationBarHidden(true,animated:true)
self.navigationItem.prompt = “正在载入……”
self.navigationItem.leftBarButtonItem =
UIBarButtonItem(barButtonSystemItem:.refresh, target:
self, action:#selector(FirstSubViewController.refresh))
self.navigationController?.navigationBar.isTranslucent= false
self.navigationController?.navigationBar.barStyle =UIBarStyle.black
self.navigationController?.navigationBar.tintColor =UIColor.orange
}
func refresh()
{
print(“刷新数据。”)
}

image.png

你可能感兴趣的:(IOS 导航栏 UINavigationController 常用)