iOS 自定义导航栏 NavigationBar
自定义一个导航栏,包括左侧、右侧按钮,中间的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相关推荐
- 微信小程序自定义导航栏navigationBar
这里使用三段论是什么为什么怎么做来阐述自定义navigationBar,如果只关心怎么做可直接跳至怎么做段落. navigationBar是什么? 微信小程序一般来说有两个bar,一个导航栏,一个ta ...
- IOS 自定义导航栏标题和返回按钮标题
IOS中自定义导航栏标题: UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)]; tit ...
- 微信小程序 ios自定义导航栏 下拉“橡皮筋” 效果
问题: 1.微信小程序ios中如果自定义导航栏取消了橡皮筋效果页面无法滑动 2.如果不取消,页面往下拉时会与顶部有一大片空白(安卓本身scrollTop不会有负值) 3.fixed之后absolute ...
- 微信小程序自定义顶部导航栏navigationBar
自定义navigationBar怎么做? 去掉原生导航栏. 将需要自定义navigationBar页面的page.json的navigationBarTitleText去掉. 加上"navi ...
- iOS个人中心渐变动画、微信对话框、标签选择器、自定义导航栏、短信验证输入框等源码...
iOS精选源码 简单的个人中心页面-自定义导航栏并予以渐变动画 程序员取悦女票的正确姿势---Tip1(iOS美容篇) iOS 前台重启应用和清除角标的问题 微信原生提醒对话框3.0 JHLikeBu ...
- IOS精品源码,仿探探UIButton封装iOS提示弹框迅速引导页自定义导航栏
1.仿 探探UI Swift ,重用机制 2.超强UIButton封装 3.一行代码集成UIPickerView,界面完全自定义 4.iOS提示弹框 5.swift UITableView / UIC ...
- 微信小程序ios滑动问题(滑动卡顿,下拉拖动自定义导航栏)
微信小程序ios滑动问题(滑动卡顿,下拉拖动自定义导航栏) 1.ios中个别机型类似6s ,对微信自带的scroll-view无法滑动: 解决:在滑动内容区添加css属性,overflow: auto ...
- iOS小技能:自定义导航栏,设置全局导航条外观。(iOS15适配)
文章目录 前言 I 自定义导航栏 1.1 自定义导航条 1.2 设置全局导航条按钮主题 1.3 设置导航条渐变颜色 1.4 ` 拦截push` II 相关代码 see also 前言 需求:同一个模块 ...
- ie9 java小程序设置_小程序 自定义导航栏
一.概念 上面整体就是自定义导航栏的区域(包括状态栏) 胶囊接口 /*获取菜单按钮(右上角胶囊按钮)的布局位置信息.坐标信息以屏幕左上角为原点 */ wx.getMenuButtonBoundingC ...
最新文章
- Sensors Data:初创公司构建数据分析平台
- 測试新浪微博@小冰 为代码机器人的一些方法
- Handler原理分析
- Handler源码解读
- linux开源游戏_2014年杰出的开源和Linux游戏
- JAVA大数据-Week4-DAY6-JDBC
- 【CSP201312-4】有趣的数(数位DP)
- 如何下载并使用别人写的库——jupyter notebook
- Pycharm安装包(类库)的方法总结及解决包下载慢的问题
- 计算机word大作业,计算机操作基础--Word大作业要求.doc
- 让自己的电脑会说话||让电脑在开机时说话
- 邬贺铨:商用一周年,5G成为中国数字经济的“新引擎”
- 800个有趣句子帮你记忆7000个单词(1-400)
- 一个简单的python爬虫程序
- 【凸优化】关于 KKT 条件 及其最优性
- Golang边无际一面
- 【设计模式】我对设计模式的C语言解读(上)
- ModelArts实现语音分类、文字类别分类
- 创新声卡系统更新连接服务器问题,创新X-Fi声卡在Win10专业版 1903更新中无法工作的问题将被修复...
- 策略与计费控制(PCC)流程与信令流程