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.
}
网友评论