let width = UIScreen.mainScreen().bounds.size.width

let height = UIScreen.mainScreen().bounds.size.height

override func viewDidLoad() {

super.viewDidLoad()

self.title = "这是标题"

self.view.backgroundColor = UIColor.yellowColor()

//自定义标题视图

//        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 44))

//        titleLabel.textAlignment = NSTextAlignment.Center

//        titleLabel.backgroundColor = UIColor.grayColor()

//        titleLabel.font = UIFont.boldSystemFontOfSize(25)

//        titleLabel.textColor = UIColor.redColor()

//        titleLabel.text = "hehe"

//        self.navigationItem.titleView = titleLabel

let button = UIButton(type: UIButtonType.System)

button.setTitle("按钮", forState: UIControlState.Normal)

button.addTarget(self, action: "titleViewBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)

button.frame = CGRect(x: 0, y: 0, width: 100, height: 30)

self.navigationItem.titleView = button

/*----------------------UINavigationItem两侧按钮----------------*/

//第一种方式

let leftItem1 = UIBarButtonItem(title: "anniu", style: UIBarButtonItemStyle.Done, target: self, action: "leftAnniuClick")

//        self.navigationItem.leftBarButtonItem = leftItem1

//第一种方式

let leftItem2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action: "btnClick:")

//        self.navigationItem.leftBarButtonItem = leftItem2

self.navigationItem.leftBarButtonItems = [leftItem1, leftItem2]

//第三种方式(自定义按钮)

let rightButton = UIButton(type: UIButtonType.Custom)

rightButton.frame = CGRect(x: 0, y: 0, width: 33, height: 32)

rightButton.addTarget(self, action: "rightBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)

rightButton.setImage(UIImage(named: "btn_search.png"), forState: UIControlState.Normal)

let rightItem = UIBarButtonItem(customView: rightButton)

self.navigationItem.rightBarButtonItem = rightItem

/*----------------UINavigationBar导航栏背景设置------------------*/

//隐藏导航栏

//        self.navigationController?.navigationBarHidden = true

//设置导航栏背景风格

self.navigationController?.navigationBar.barStyle = UIBarStyle.Default

//设置导航栏的半透明效果,默认是yes

//        self.navigationController?.navigationBar.translucent = false

//设置导航栏的背景颜色(必须有半透明效果)

//        self.navigationController?.navigationBar.backgroundColor = UIColor.redColor()

//将导航上所有的Label的字体变色

self.navigationController?.navigationBar.tintColor = UIColor.redColor()

//设置图片作为导航栏的背景,设置了背景图片,导航栏就不透明

var img = UIImage(named: "navbar_bg_normal.png")

UIGraphicsBeginImageContext(CGSizeMake(width, 64))

img?.drawInRect(CGRect(x: 0, y: 0, width: width, height: 64))

img = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

self.navigationController?.navigationBar.setBackgroundImage(img, forBarMetrics: UIBarMetrics.Default)

//设置提示文本,通常用不上

//        self.navigationItem.prompt = "正在保存";

}

func titleViewBtnClick(btn: UIButton) {

print("标题被点击了")

}

func leftAnniuClick() {

print("anniu被点击了")

}

func btnClick(btn: UIButton) {

print(btn)

}

func rightBtnClick(btn: UIButton) {

print("rightBtnClick")

}

Swift 导航栏设置的更多相关文章

  1. Swift 导航栏设置图片点击事件,图片蓝色的解决方案

    如果导航栏想做一个点击事件,正好是一个图片 我们可以直接这样: self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIIm ...

  2. ABP(现代ASP.NET样板开发框架)系列之22、ABP展现层——导航栏设置

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之22.ABP展现层——导航栏设置 ABP是“ASP.NET Boilerplate Project (ASP.NE ...

  3. 转:ios导航栏设置

    原帖:http://www.cocoachina.com/industry/20131104/7287.html 本文提供的代码需要用Xcode 5来执行.如果你还在使用老版本的Xcode,那么在运行 ...

  4. 前端css小米导航栏设置及盒子定位居中问题

    1.小米最上部导航栏设置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. swift导航栏导航按钮添加多个按钮事件

    //导航左边返回按钮 let button1 = UIButton(frame:CGRectMake(0, 0, 18, 18)) button1.setImage(Constant.Image.Na ...

  6. iOS 11导航栏设置BarButtonItem变形问题和错位问题

    升级到 iOS 11,你可能会发现你的 App 的工具栏的行为出了点异常.比如奇点,我在使用过程中,发现工具栏时灵时不灵,感觉很难点到.这是怎么回事?通过 Xcode 的 Debug View Hie ...

  7. iOS - 导航栏设置半透明或取消半透明

    self.navigationController.navigationBar.translucent = YES;//透明

  8. iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

    一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...

  9. iOS10 的适配问题,你遇到了吗?导航栏标题和返回按钮神奇的消失了

    苹果系统升级后好多应用都发了新版本来适配,今天就来分享一下我的适配历程. 首先是出现的问题: 1.push一个控制器,返回按钮和标题神奇的消失了,打开三维视图(比较坑的是有的版本老到打不开三维视图 ) ...

随机推荐

  1. Docker Compose 入门使用指南

    Compose is a tool for defining and running multi-container Docker applications. With Compose, you us ...

  2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

    一.什么是跨域访问 举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请 ...

  3. VirtualXposed查看手机端网页及调试

    参考博客: https://sspai.com/post/44447 以下是实际动手操作步骤: 一 打开手机开发者模式 二 允许USB调试 三 安装  VirtualXposed_1.apk      ...

  4. vue版本,小Toast

    <div id="message" :class="{'show':show_Message}"><p v-html="messag ...

  5. ubuntu16.04(liunx) 离线安装 xgboost (anaconda3,anaconda2共存)

    服务器ubuntu 系统同时安装了 anaconda3,anaconda2 ,但服务器没有连接外网,所以 想在python3 环境下安装离线安装xgboost. 主要分2步: 0:进入py3环境  ( ...

  6. 实现Vue-MVVM-step1

    一个利用defineProperty实现的MVVM双向数据绑 <!DOCTYPE html> <html lang="en"> <head> & ...

  7. Git-标签管理【转】

    本文转载自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 标签管理 发布一个版本 ...

  8. pip安装tensorflow-gpu好慢怎么办

    答:为pip换源,如换成清华源 cat ~/.pip/pip.conf(没有此文件,自行创建即可,然后加入以下内容) [global]index-url = https://pypi.tuna.tsi ...

  9. java maven 操作 收集的一些命令

    maven打包: mvn clean package -Dmaven.test.skip=true 运行jar: java -jar target/spring-boot-scheduler-1.0. ...

  10. Flyweight(享元)

    意图: 运用共享技术有效地支持大量细粒度的对象. 适用性: 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态,那 ...