import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//设置导航栏的标题
//每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里面包含了类左按钮,右按钮,中间标题,中间视图)
navigationItem.title = "Setting"
//设置导航栏左按钮
let letfBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "letfBtnAction")
let rightBarBtn = UIBarButtonItem(title: "next", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBtnAction")
// let rightBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Rewind, target: self, action: "rightBtnAction")
navigationItem.leftBarButtonItem = letfBarBtn
navigationItem.rightBarButtonItem = rightBarBtn
// navigationItem.leftBarButtonItems = [letfBarBtn,rightBarBtn]
// navigationItem.rightBarButtonItems = [rightBarBtn,letfBarBtn]
//设置中间视图
let segment = UISegmentedControl(items: ["已接来电","未接来电"])
segment.frame = CGRectMake(0, 0, 100, 30)
segment.selectedSegmentIndex = 0
navigationItem.titleView = segment
//导航栏(UINavigationBar)
//在本类中(视图控制器)访问navigationController就是获取到本视图控制器所在的导航视图控制器
//设置导航栏是否隐藏
navigationController?.navigationBarHidden = false
//设置导航栏样式
navigationController?.navigationBar.barStyle = UIBarStyle.Default
//背景颜色
navigationController?.navigationBar.backgroundColor = UIColor.cyanColor()
//导航栏本身的颜色
navigationController?.navigationBar.barTintColor = UIColor.yellowColor()
//导航栏元素颜色(左按钮 右按钮 中间标题........)
navigationController?.navigationBar.tintColor = UIColor.redColor()
//导航栏半透明效果
navigationController?.navigationBar.translucent = true
let myView = UIView(frame: CGRectMake(0, 0, 150, 150))
myView.backgroundColor = UIColor.blueColor()
view.addSubview(myView)
}
func letfBtnAction(){
print("dfsfdsf")
}
//跳转第二个控制器页面
func rightBtnAction(){
//(1)创建第二个控制器
let secondVC = SecondViewController()
//(2)使用当前控制所在的导航视图控制器跳转到第二个控制器pushViewController(进入到下一个页面)
navigationController?.pushViewController(secondVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//设置页面颜色为白色
view.backgroundColor = UIColor.whiteColor()
navigationItem.title = "SecondVC"
let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
let rightBarBtn = UIBarButtonItem(title: "next", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBtnAction")
navigationItem.leftBarButtonItem = leftBarBtn
navigationItem.rightBarButtonItem = rightBarBtn
// Do any additional setup after loading the view.
}
func backAction(btn:UIBarButtonItem){
print("111")
//将secondVC出輚
//(回到上一个页面)将SecondVC出輚popViewControllerAnimated:将当前显示在栈顶的控制器出輚
// navigationController?.popViewControllerAnimated(true)
//先获取到棧里所有的视图控制器
let viewControllers = navigationController?.viewControllers
//获取根视图控制器(因为根视图控制器是最先入栈,所以在第0个下标)
let rootVC: AnyObject = viewControllers![0]
//导航视图控制器返回到指定的视图控制器
navigationController?.popToViewController(rootVC as! UIViewController, animated: true)
}
func rightBtnAction(){
let ThirdVC = ThirdViewController()
navigationController?.pushViewController(ThirdVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
import UIKit
class ThirdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myBtn = UIButton(frame: CGRectMake(100, 130, 100, 45))
myBtn.setTitle("模态显示", forState: UIControlState.Normal)
myBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
myBtn.backgroundColor = UIColor.redColor()
myBtn.addTarget(self, action: "presentToFifes", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(myBtn)
let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
// Do any additional setup after loading the view.
}
func backAction(btn:UIBarButtonItem){
print("222")
navigationController?.popViewControllerAnimated(true)
}
func presentToFifes(){
print("模态")
let foursVC = FoursViewController()
//模态显示,跟导航视图控制器没关系
//参数completion:模态显示完成后要执行的闭包
presentViewController(foursVC, animated: true) { () -> Void in
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
import UIKit
class FoursViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.cyanColor()
let modelBtn = UIButton(frame: CGRectMake(80, 150, 80, 45))
modelBtn.setTitle("模态消失", forState: UIControlState.Normal)
modelBtn.backgroundColor = UIColor.whiteColor()
modelBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
modelBtn.addTarget(self, action: "dismissViewController", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(modelBtn)
// Do any additional setup after loading the view.
}
func dismissViewController(){
//1.第一种方式:dismissViewController()模态消失过程不可定制化
//2.第二种方式:模态消失过程可定制化(需不需要动画,模态结束后执行代码段 )
dismissViewControllerAnimated(true, completion: { () -> Void in
print("模态消失动作已经结束")
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}