swift實(shí)現(xiàn)導(dǎo)航欄
funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool{
1根據(jù)一個根視圖控制器創(chuàng)建一個導(dǎo)航視圖控制器
let vc =ViewController()
let navc =UINavigationController(rootViewController: vc)
2將應(yīng)用的根視圖控制器設(shè)置為導(dǎo)航視圖控制器
window=UIWindow(frame:UIScreen.mainScreen().bounds)
window?.rootViewController= navc
window?.backgroundColor=UIColor.whiteColor()
window?.makeKeyAndVisible()
return true
}
funcapplicationWillResignActive(application:UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application:UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application:UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application:UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application:UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
//每一個被導(dǎo)航視圖控制器所管理的視圖控制器都有一個navigationItem(這里面包含了左按鈕晚碾,右按鈕,中間標(biāo)題址儒,中間視圖)
//設(shè)置導(dǎo)航欄的標(biāo)題
navigationItem.title="Setting"
let leftBarBtn =UIBarButtonItem(barButtonSystemItem: .Camera, target:self, action:"leftBtnAction")
//設(shè)置右邊按鈕
let rightBarBtn =UIBarButtonItem(title:"next", style:UIBarButtonItemStyle.Plain, target:self, action:"rightBtnAction")
//設(shè)置導(dǎo)航欄左按鈕leftBarButtonItem:(UIBarButtonItem)
navigationItem.leftBarButtonItem= leftBarBtn
navigationItem.rightBarButtonItem= rightBarBtn
//設(shè)置左右item數(shù)組
//navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]
//navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]
//設(shè)置中間視圖
letsegment =UISegmentedControl(items: ["已接來電","未接來dian"])
segment.frame=CGRectMake(0,0,100,30)
segment.selectedSegmentIndex=1
//設(shè)置中間視圖
navigationItem.titleView= segment
//導(dǎo)航欄(UINavigationBar)
//在本類中(視圖控制器)訪問navigationController就是獲取到本視圖控制器所在的導(dǎo)航視圖控制器
//設(shè)置導(dǎo)航欄是否隱藏
navigationController?.navigationBarHidden=false
//設(shè)置導(dǎo)航欄樣式
navigationController?.navigationBar.barStyle= .Default
//背景顏色
navigationController?.navigationBar.backgroundColor=UIColor.cyanColor()
//導(dǎo)航欄本身的顏色
navigationController?.navigationBar.barTintColor=UIColor.yellowColor()
//導(dǎo)航欄元素顏色(左按鈕,右按鈕.........)
navigationController?.navigationBar.tintColor=UIColor.redColor()
//導(dǎo)航欄半透明效果
navigationController?.navigationBar.translucent=false
let myView =UIView(frame:CGRectMake(0,0,150,150))
myView.backgroundColor=UIColor.blueColor()
view.addSubview(myView)
//navigationController的contentView顯示的誰的View座每?
}
//跳轉(zhuǎn)第二個控制器頁面
funcrightBtnAction(){
//(1)創(chuàng)建第二個控制器
let secondVC =SecondViewController()
//(2)使用當(dāng)前控制器所在的導(dǎo)航視圖控制器跳轉(zhuǎn)到第二個控制器pushViewController(進(jìn)入到下一個頁面)
navigationController?.pushViewController(secondVC, animated:true)
}
func leftBtnAction(){
print("click left Btn")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}