導(dǎo)航欄的設(shè)置與處理
*首先創(chuàng)建一個(gè)視圖,并創(chuàng)建好導(dǎo)航欄管理控件
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//創(chuàng)建一個(gè)窗口
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
//使窗口可見
self.window?.makeKeyAndVisible()
//關(guān)聯(lián)ViewController
let viewCrt = ViewController()
//創(chuàng)建導(dǎo)航欄管理器
let navCrt = UINavigationController(rootViewController: viewCrt)
self.window?.rootViewController = navCrt
//設(shè)置導(dǎo)航欄顏色
navCrt.navigationBar.barTintColor = UIColor.redColor()
//設(shè)置導(dǎo)航欄上所有添加上去的視圖顏色(包括label和button/image)
navCrt.navigationBar.tintColor = UIColor.blackColor()
return true
}
*然后進(jìn)行添加導(dǎo)航欄上的東西
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
//創(chuàng)建一個(gè)系統(tǒng)button
let button = UIButton(type: .System)
button.frame = CGRect(x: self.view.frame.size.width / 2 - 50, y: self.view.frame.size.height / 2 - 25, width: 100, height: 50)
button.tintColor = UIColor.redColor()
button.setTitle("first page", forState: .Normal)
button.addTarget(self, action: #selector(didClick(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(button)
//設(shè)置導(dǎo)航欄上的中間title內(nèi)容,默認(rèn)為居中
self.navigationItem.title = "首頁(yè)"
//設(shè)置系統(tǒng)的圖標(biāo)擺放至導(dǎo)航欄左側(cè)
// self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: #selector(didClick(_:)))
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .Done, target: self, action: nil)
//設(shè)置導(dǎo)航欄右側(cè)內(nèi)容,添加一個(gè)button,點(diǎn)擊觸發(fā)事件為didClick
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "下一頁(yè)", style: .Plain, target: self, action: #selector(didClick(_:)))
}
func didClick(sender: UIButton){
let seconde = secondeViewController()
//以棧的形式將第二個(gè)頁(yè)面彈出(push)
self.navigationController?.pushViewController(seconde, animated: true)
}
deinit {
print("first")
}
}
*觸發(fā)點(diǎn)擊事件,創(chuàng)建第二個(gè)頁(yè)面,第二個(gè)頁(yè)面的代碼如下:
import UIKit
class secondeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.greenColor()
}
//設(shè)置默認(rèn)的返回點(diǎn)擊頁(yè)面(以棧的方式將之前的頁(yè)面返回,pop)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.navigationController?.popViewControllerAnimated(true)
}
deinit {
print("second")
}
}