自定义一个导航栏,包括左侧、右侧按钮,中间的title。

效果图:

代码:
Swift版

// 创建一个导航栏
let navBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44))
// 导航栏背景颜色
navBar.barTintColor = UIColor(red: 101/255.0, green: 215/255.0, blue: 237/255.0, alpha: 1.0)// 自定义导航栏的title,用UILabel实现
let titleLabel = UILabel(frame: CGRectMake(0, 0, 50, 44))
titleLabel.text = "自定义"
titleLabel.textColor = UIColor.whiteColor()
titleLabel.font = UIFont.systemFontOfSize(18)// 创建导航栏组件
let navItem = UINavigationItem()
// 设置自定义的title
navItem.titleView = titleLabel// 创建左侧按钮
let leftButton = UIBarButtonItem(title: "leftButton", style: .Plain, target: self, action: "leftButtonClick")
leftButton.tintColor = UIColor.purpleColor()// 创建右侧按钮
let rightButton = UIBarButtonItem(title: "rightButton", style: .Plain, target: self, action: "rightButtonClick")
rightButton.tintColor = UIColor.orangeColor()// 添加左侧、右侧按钮
navItem.setLeftBarButtonItem(leftButton, animated: false)
navItem.setRightBarButtonItem(rightButton, animated: false)
// 把导航栏组件加入导航栏
navBar.pushNavigationItem(navItem, animated: false)// 导航栏添加到view上
self.view.addSubview(navBar)

Objective-C版

// 创建一个导航栏
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
// 导航栏背景颜色
navBar.barTintColor = [UIColor colorWithRed:101/255.0 green:215/255.0 blue:237/255.0 alpha:1.0];// 自定义导航栏的title,用UILabel实现
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
titleLabel.text = @"自定义";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont systemFontOfSize:18];// 创建导航栏组件
UINavigationItem *navItem = [[UINavigationItem alloc] init];
// 设置自定义的title
navItem.titleView = titleLabel;// 创建左侧按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"leftButton" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
leftButton.tintColor = [UIColor purpleColor];// 创建右侧按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"rightButton" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];
rightButton.tintColor = [UIColor orangeColor];// 添加左侧、右侧按钮
[navItem setLeftBarButtonItem:leftButton animated:false];
[navItem setRightBarButtonItem:rightButton animated:false];
// 把导航栏组件加入导航栏
[navBar pushNavigationItem:navItem animated:false];// 导航栏添加到view上
[self.view addSubview:navBar];

iOS 自定义导航栏 NavigationBar相关推荐

  1. 微信小程序自定义导航栏navigationBar

    这里使用三段论是什么为什么怎么做来阐述自定义navigationBar,如果只关心怎么做可直接跳至怎么做段落. navigationBar是什么? 微信小程序一般来说有两个bar,一个导航栏,一个ta ...

  2. IOS 自定义导航栏标题和返回按钮标题

    IOS中自定义导航栏标题: UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)]; tit ...

  3. 微信小程序 ios自定义导航栏 下拉“橡皮筋” 效果

    问题: 1.微信小程序ios中如果自定义导航栏取消了橡皮筋效果页面无法滑动 2.如果不取消,页面往下拉时会与顶部有一大片空白(安卓本身scrollTop不会有负值) 3.fixed之后absolute ...

  4. 微信小程序自定义顶部导航栏navigationBar

    自定义navigationBar怎么做? 去掉原生导航栏. 将需要自定义navigationBar页面的page.json的navigationBarTitleText去掉. 加上"navi ...

  5. iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码...

    iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...

  6. IOS精品源码,仿探探UIButton封装iOS提示弹框迅速引导页自定义导航栏

    1.仿 探探UI Swift ,重用机制 2.超强UIButton封装 3.一行代码集成UIPickerView,界面完全自定义 4.iOS提示弹框 5.swift UITableView / UIC ...

  7. 微信小程序ios滑动问题(滑动卡顿,下拉拖动自定义导航栏)

    微信小程序ios滑动问题(滑动卡顿,下拉拖动自定义导航栏) 1.ios中个别机型类似6s ,对微信自带的scroll-view无法滑动: 解决:在滑动内容区添加css属性,overflow: auto ...

  8. iOS小技能:自定义导航栏,设置全局导航条外观。(iOS15适配)

    文章目录 前言 I 自定义导航栏 1.1 自定义导航条 1.2 设置全局导航条按钮主题 1.3 设置导航条渐变颜色 1.4 ` 拦截push` II 相关代码 see also 前言 需求:同一个模块 ...

  9. ie9 java小程序设置_小程序 自定义导航栏

    一.概念 上面整体就是自定义导航栏的区域(包括状态栏) 胶囊接口 /*获取菜单按钮(右上角胶囊按钮)的布局位置信息.坐标信息以屏幕左上角为原点 */ wx.getMenuButtonBoundingC ...

最新文章

  1. Sensors Data:初创公司构建数据分析平台
  2. 測试新浪微博@小冰 为代码机器人的一些方法
  3. Handler原理分析
  4. Handler源码解读
  5. linux开源游戏_2014年杰出的开源和Linux游戏
  6. JAVA大数据-Week4-DAY6-JDBC
  7. 【CSP201312-4】有趣的数(数位DP)
  8. 如何下载并使用别人写的库——jupyter notebook
  9. Pycharm安装包(类库)的方法总结及解决包下载慢的问题
  10. 计算机word大作业,计算机操作基础--Word大作业要求.doc
  11. 让自己的电脑会说话||让电脑在开机时说话
  12. 邬贺铨:商用一周年,5G成为中国数字经济的“新引擎”
  13. 800个有趣句子帮你记忆7000个单词(1-400)
  14. 一个简单的python爬虫程序
  15. 【凸优化】关于 KKT 条件 及其最优性
  16. Golang边无际一面
  17. 【设计模式】我对设计模式的C语言解读(上)
  18. ModelArts实现语音分类、文字类别分类
  19. 创新声卡系统更新连接服务器问题,创新X-Fi声卡在Win10专业版 1903更新中无法工作的问题将被修复...
  20. 策略与计费控制(PCC)流程与信令流程

热门文章

  1. linux insert最后一行,insert基础用法及进阶
  2. 深度学习——Dual Regression Networks for Single Image Super-Resolution(DRN)
  3. AI视频增强 -- Topaz 视频超分 | Topaz Video Enhance AI | 【软件试用】
  4. Python自动化测试框架
  5. 网安刮起东风:360、深信服决战智能化
  6. 如何用scanf语句为字符指针数组赋值
  7. 微信小程序 图片等比例缩-放(图片自适应屏幕)
  8. 入职腾讯第九年,我辞职了!
  9. RAxML下载与使用与ML建树原理
  10. 想要远程办公,这些一定要知道