导航栏
//ViewController.swift
//UINavigationController
//
//Created by lanou on 16/11/2.
//Copyright (c) 2016年lanou. All rights reserved.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里包含了左按钮,右按钮,中间标题,中间视图)
//设置导航栏标题
navigationItem.title = "Setting"
let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")
//设置导航栏左按钮(UIBarButtonItem)
navigationItem.leftBarButtonItem = leftBarBtn
let rightBarBtn = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "rightBtnAction")
navigationItem.rightBarButtonItem = rightBarBtn
// navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]
// navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]
//设置中间视图
let segment = UISegmentedControl(items: ["已接来电","未接来电"])
segment.frame = CGRectMake(0, 0, 100, 30)
segment.selectedSegmentIndex = 0
navigationItem.titleView = segment
//导航栏(UINavigationBar)
//在本类中(视图控制器)访问navigationController就是获取的到本视图控制器所在的导航视图控制器
//设置导航栏是否隐藏
navigationController?.navigationBarHidden = false
//设置导航栏的样式
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, 60, 60))
myView.backgroundColor = UIColor.blueColor()
view.addSubview(myView)
}
func leftBtnAction(){
print("click left Btn")
}
//跳转到第二个控制器的页面
func rightBtnAction(){
//(1)创建第二个控制器
let secondVC = secondViewController()
//(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器,pushViewController可以进入到下一个页面
navigationController?.pushViewController(secondVC, animated: true)
print("click right Btn")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//secondViewController.swift
//UINavigationController
//
//Created by lanou on 16/11/3.
//Copyright (c) 2016年lanou. All rights reserved.
import UIKit
class secondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "seconrVC"
//设置颜色
view.backgroundColor = UIColor.whiteColor()
let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
navigationItem.leftBarButtonItem = leftBarBtn
}
func backAction(btn:UIBarButtonItem){
println("返回")
//将secondVC出栈,popViewControllerAnimated:将当前的显示在栈顶的控制器出栈
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
// FifthViewController.swift
// UINavigationController
//
// Created by SZT on 2016/11/3.
// Copyright © 2016年 SZT. All rights reserved.
//
import UIKit
class FifthViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
navigationItem.title = "FifthVC"
let leftBtn = UIBarButtonItem(title: "backToRoot", style: UIBarButtonItemStyle.Plain, target: self, action: "popToRootViewController")
navigationItem.leftBarButtonItem = leftBtn
let btn = UIButton(frame: CGRectMake(100,130,80,45))
btn.setTitle("模态显示", forState: .Normal)
btn.backgroundColor = UIColor.cyanColor()
btn.addTarget(self, action: "presentToSix", forControlEvents: .TouchUpInside)
view.addSubview(btn)
}
//点击按钮模态显示第六个视图控制器
func presentToSix(){
//创建第六个视图控制器
let sixthVC = SixthViewController()
//模态显示,跟导航视图控制器没关系
//参数completion:模态显示完成之后要执行的闭包
presentViewController(sixthVC, animated: true) { () -> Void in
//模态显示动作完成要执行的代码
print("模态动作已完成")
}
}
func popToRootViewController(){
//(1)popToRootViewControllerAnimated:回到根视图控制器
// navigationController?.popToRootViewControllerAnimated(true)
// (2)
//先获取到棧里所有的视图控制器
let viewControllers = navigationController?.viewControllers
//获取根视图控制器(因为根视图控制器是最先入棧,所以在第0个下标)
let rootVC:UIViewController = viewControllers![0]
//导航视图控制器返回到指定的视图控制器
navigationController?.popToViewController(rootVC, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//SixthViewController.swift
//UINavigationController
//
//Created by SZT on 2016/11/3.
//Copyright © 2016年SZT. All rights reserved.
import UIKit
class SixthViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.grayColor()
let modelBtn = UIButton(frame: CGRectMake(80,150,100,45))
modelBtn.setTitle("模态消失", forState: .Normal)
modelBtn.backgroundColor = UIColor.blueColor()
modelBtn.addTarget(self, action: "dismissViewcontroller", forControlEvents: .TouchUpInside)
view.addSubview(modelBtn)
}
func dismissViewcontroller(){
// (1)第一种方式:模态过程不可定制化 dismissViewcontroller()
//(2)第二种方式:模态消失过程可定制化(需不需要动画,模态结束后执行代码段)
dismissViewControllerAnimated(true) { () -> 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.
}
*/
}
/*
}
*/
}
/*
页面跳转的方式
1.模态显示
//创建第六个视图控制器
letsixthVC = SixthViewController()
//模态显示,跟导航视图控制器没关系
//参数completion:模态显示完成之后要执行的闭包
presentViewController(sixthVC, animated:true) { () ->Voidin
//模态显示动作完成要执行的代码
print("模态动作已完成")
}
2.push
*/