無標題文章

swift實現導航欄


funcapplication(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) ->Bool{

1根據一個根視圖控制器創建一個導航視圖控制器

let vc =ViewController()

let navc =UINavigationController(rootViewController: vc)

2將應用的根視圖控制器設置為導航視圖控制器

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:.

}

//每一個被導航視圖控制器所管理的視圖控制器都有一個navigationItem(這里面包含了左按鈕,右按鈕,中間標題,中間視圖)

//設置導航欄的標題

navigationItem.title="Setting"

let leftBarBtn =UIBarButtonItem(barButtonSystemItem: .Camera, target:self, action:"leftBtnAction")

//設置右邊按鈕

let rightBarBtn =UIBarButtonItem(title:"next", style:UIBarButtonItemStyle.Plain, target:self, action:"rightBtnAction")

//設置導航欄左按鈕leftBarButtonItem:(UIBarButtonItem)

navigationItem.leftBarButtonItem= leftBarBtn

navigationItem.rightBarButtonItem= rightBarBtn

//設置左右item數組

//navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]

//navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]

//設置中間視圖

letsegment =UISegmentedControl(items: ["已接來電","未接來dian"])

segment.frame=CGRectMake(0,0,100,30)

segment.selectedSegmentIndex=1

//設置中間視圖

navigationItem.titleView= segment

//導航欄(UINavigationBar)

//在本類中(視圖控制器)訪問navigationController就是獲取到本視圖控制器所在的導航視圖控制器

//設置導航欄是否隱藏

navigationController?.navigationBarHidden=false

//設置導航欄樣式

navigationController?.navigationBar.barStyle= .Default

//背景顏色

navigationController?.navigationBar.backgroundColor=UIColor.cyanColor()

//導航欄本身的顏色

navigationController?.navigationBar.barTintColor=UIColor.yellowColor()

//導航欄元素顏色(左按鈕,右按鈕.........)

navigationController?.navigationBar.tintColor=UIColor.redColor()

//導航欄半透明效果

navigationController?.navigationBar.translucent=false

let myView =UIView(frame:CGRectMake(0,0,150,150))

myView.backgroundColor=UIColor.blueColor()

view.addSubview(myView)

//navigationController的contentView顯示的誰的View?

}

//跳轉第二個控制器頁面

funcrightBtnAction(){

//(1)創建第二個控制器

let secondVC =SecondViewController()

//(2)使用當前控制器所在的導航視圖控制器跳轉到第二個控制器pushViewController(進入到下一個頁面)

navigationController?.pushViewController(secondVC, animated:true)

}

func leftBtnAction(){

print("click left Btn")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容