8.12 导航栏Navigation

不使用storyboard布局,将其删除,全代码写

在appDelegate中创建窗口

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //1. 创建窗口
  self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //2. 显示窗口
        self.window?.makeKeyAndVisible()
        let viewCtrl = ViewController()
//        self.window?.rootViewController = viewCtrl

        //3. 创建导航控制器,至少需要放入一个页面(第一个页面也叫做根视图控制器)
        let navCtrl = UINavigationController(rootViewController: viewCtrl)

        //文字/图片
        navCtrl.navigationBar.tintColor = UIColor.redColor()

        //整个导航条的颜色
        navCtrl.navigationBar.barTintColor = UIColor.greenColor()
        self.window?.rootViewController = navCtrl
        return true
    }

ViewController中


import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.redColor()
//        self.title = "第一页"
        //用于设置导航条
//        self.navigationItem.title = "第一页"
        //设置导航条中间部分
        let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 10))
        v.backgroundColor = UIColor.blueColor()
        self.navigationItem.titleView = v
        //设置左侧
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "左侧", style: .Plain, target: self, action: #selector(leftClicked))

//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Bookmarks, target: self, action: #selector(leftClicked))
        let originImage = UIImage(named: "1.png")

        //图片总是显示原始颜色

        let image = originImage?.imageWithRenderingMode(.AlwaysOriginal)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(leftClicked))
        let btn = UIButton(type: .System)
        btn.frame = CGRect(x: self.view.frame.size.width / 2 - 75, y:self.view.frame.size.height / 2 - 25, width: 150, height: 50)
        btn.setTitle("下一页", forState: .Normal)
        btn.addTarget(self, action: #selector(didClicked(_:)), forControlEvents: .TouchUpInside)
        self.view.addSubview(btn)
    }

    func leftClicked() {
        print("leftClicked")
    }

    func didClicked(sender: UIButton) {
        let secondCtrl = SecondViewController()
        //每一个“已经”处于导航控制器中的页面,它的navigationController属性不为空
        self.navigationController?.pushViewController(secondCtrl, animated: true)
    }
    deinit {
        print("first")
    }
}

新建第二个SecondViewControl

import UIKit

class SecondViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置背景色
        self.view.backgroundColor = UIColor.greenColor()
    }

    //点击任意地方返回第一页
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.navigationController?.popViewControllerAnimated(true)
    }
    //返回后让其销毁
    deinit {
        print("second")
    }
}

PS:导航栏的默认高度为66个像素,上面默认留20个像素(时间,电量部分),下面44个像素

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.尺寸适配1.原因 iOS7中所有导航栏都为半透明,导航栏(height=44)和状态栏(height=20)不...
    LZM轮回阅读 6,150评论 1 4
  • Storyboard是最先在iOS 5引入的一项振奋人心的特性,大幅缩减构建App用户界面所需的时间。 要介绍St...
    余一波_Bobby阅读 61,910评论 11 76
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,494评论 2 50
  • 雪和太阳 雪热爱太阳。为了拥抱阳光,不惜停止飞扬。太阳终于出现了,却一脸惆怅。他看着雪被自己灼伤,心痛地没了声响。...
    又又周阅读 771评论 1 1
  • 人没有办法决定出生,可是却有办法决定死亡。决定死亡时自己是个什么样的人。 这两天睡眠特别好,依旧会做梦,可是醒来时...
    Amy_dandan阅读 197评论 0 0