無標(biāo)題文章

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.

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末琉朽,一起剝皮案震驚了整個濱河市域帐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌悴侵,老刑警劉巖瞧剖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異可免,居然都是意外死亡筒繁,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門巴元,熙熙樓的掌柜王于貴愁眉苦臉地迎上來毡咏,“玉大人,你說我怎么就攤上這事逮刨∨荤裕” “怎么了堵泽?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長恢总。 經(jīng)常有香客問我迎罗,道長,這世上最難降的妖魔是什么片仿? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任纹安,我火速辦了婚禮,結(jié)果婚禮上砂豌,老公的妹妹穿的比我還像新娘厢岂。我一直安慰自己,他們只是感情好阳距,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布塔粒。 她就那樣靜靜地躺著,像睡著了一般筐摘。 火紅的嫁衣襯著肌膚如雪卒茬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天咖熟,我揣著相機(jī)與錄音圃酵,去河邊找鬼。 笑死馍管,一個胖子當(dāng)著我的面吹牛郭赐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咽斧,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼堪置,長吁一口氣:“原來是場噩夢啊……” “哼躬存!你這毒婦竟也來了张惹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤岭洲,失蹤者是張志新(化名)和其女友劉穎宛逗,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盾剩,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡雷激,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了告私。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片屎暇。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖驻粟,靈堂內(nèi)的尸體忽然破棺而出根悼,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布挤巡,位于F島的核電站剩彬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏矿卑。R本人自食惡果不足惜折晦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一豺型、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦还蹲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至椎咧,卻和暖如春玖详,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勤讽。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工蟋座, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人脚牍。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓向臀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親诸狭。 傳聞我的和親對象是個殘疾皇子券膀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容