本章实现效果:

Untitled.gif

前言:

项目中很多需求是要求自定义标题栏居中的,本人最近就遇到这中需求,如果用系统自带的titleView设置的话,不会居中,经过尝试,发现titleview的起点位置和尺寸依赖于leftBarButtonItem和rightBarButtonItem的位置。下面给出我的解决方案

首先自定义一个标题View
#import <UIKit/UIKit.h>
@interface CustomTitleView : UIView @property (nonatomic, copy) NSString *title; @end #import "CustomTitleView.h" #import "Masonry.h" @interface CustomTitleView () @property(nonatomic,strong)UILabel * titleLabel;//标题label @property (nonatomic,strong) UIView *contentView; @end @implementation CustomTitleView - (instancetype)init { self = [super init]; if (self) { [self addSubview:self.contentView]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.greaterThanOrEqualTo(self); make.right.lessThanOrEqualTo(self); make.center.equalTo(self); make.bottom.top.equalTo(self); }]; [self.contentView addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.contentView); make.centerX.equalTo(self.contentView); }]; } return self; } - (void)setFrame:(CGRect)frame { [super setFrame:frame]; [self layoutIfNeeded]; } - (UIView *)contentView { if (!_contentView) { _contentView = [UIView new]; } return _contentView; } -(UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.font = [UIFont boldSystemFontOfSize:17]; _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; _titleLabel.textAlignment = NSTextAlignmentCenter; [_titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; _titleLabel.backgroundColor = [UIColor redColor]; } return _titleLabel; } - (void)setTitle:(NSString *)title { self.titleLabel.text = title; }
具体用法如下:

在当前页面的控制中只要写,即可实现上图的效果

  CustomTitleView *titleView = [[CustomTitleView alloc] init];titleView.backgroundColor = [UIColor greenColor];titleView.frame = CGRectMake(0, 0, PDScreeenW, 44); titleView.title = @"我是标题"; self.navigationItem.titleView = titleView; self.titleView = titleView; UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil]; self.navigationItem.rightBarButtonItem = rightBarButtonItem;

iOS - 设置导航栏之标题栏居中、标题栏的背景颜色相关推荐

  1. IOS设置导航栏返回按钮,并添加事件返回主页面

    IOS设置导航栏返回按钮,并添加事件返回主页面 前提是已经push了一个viewController了.才能使用. XXXTableViewController 里面书写 - (void)viewDi ...

  2. IOS设置导航栏的背景图片和文字

    IOS设置导航栏的背景图片和文字 - (void)viewDidLoad {[super viewDidLoad];[self.navigationBar setBackgroundImage:[UI ...

  3. iOS设置导航栏和状态栏

    文章目录 iOS 15 之后导航栏背景色的设置 1.状态栏设置 1.1.没有导航栏 1.2.有导航栏 2.导航栏背景和字体颜色 2.1.十六进制颜色转RGB 2.2.生成纯色图片 3.导航栏的另外一种 ...

  4. iOS 设置导航栏背景颜色

    //导航栏背景色UIColor *navBarColor = [UIColor colorWithRGB_au:0xD42D37];if (@available(iOS 15.0, *)) {UINa ...

  5. iOS与导航栏有关的神技

      1.导航栏上的返回按钮 <span style="font-size:24px;"> </span><span style="font- ...

  6. uniapp动态设置导航栏和标题栏语言

    hbuilderx中uniapp动态设置导航栏和标题栏语言 一 .下载vue-i18n文件引入项目 hbuilder本身是带有能够运行nmp命令的终端,由于用不到全部文件,我采用的是在电脑cmd窗口下 ...

  7. iOS 解决导航栏pop返回时出现黑块问题!

    iOS 解决导航栏pop返回时出现黑块问题! 问题描述: 导航栏正常从A页面push到B页面,从B页面pop返回A页面时遇到过渡过程中导航栏出现黑块的问题. 如截图所示: 问题原因: A界面导航栏被影 ...

  8. 控制iOS的导航栏和状态栏的样式

    这是一个很常用的开发场景,就是改变导航栏上的文字颜色与背景色,如果你曾有 windows form 开发经验一定会笑我:"卧槽,这有什么好写的,不就是设置两个属性就可以了吗?" 我 ...

  9. ios 添加导航栏视图_iOS进度栏(进度视图)

    ios 添加导航栏视图 In this tutorial, we'll be discussing the UIProgressView component and create a progress ...

最新文章

  1. 基于深度学习网络的运动想象BCI系统及其应用
  2. 软件工程之“个人附加题”
  3. LR 场景选项配置--笔记
  4. mysql update返回_MySQL中,当update修改数据与原数据相同时会再次执行吗?
  5. Android的ELF文件重定位详解,包括64位
  6. python3.7界面_Python3.7+tkinter实现查询界面功能
  7. java 判断二级网址_【Java】利用正則表達式推断是否为网址
  8. 13.看板方法---使用两层系统扩展看板
  9. Apple原装扫描二维码指定区域
  10. (转)用Wineskin 让Windows 的程序在Mac 上运行
  11. python共轭梯度法_Numerical Analysis: 共轭梯度法(1)--基本原理
  12. 连续函数零点定理 介值定理
  13. 2020年数学建模亚太赛赛后分享总结
  14. 致我们终将远离的子女
  15. 【精简】海姆利克急救法+心肺复苏 基础急救技能
  16. 【OpenGL 学习笔记】第 4 篇:绘制四边形
  17. java.lang.IllegalArgumentException异常
  18. 认识微型计算机的组成ppt,认识微型计算机(PPT课件).ppt
  19. 电子管功放制作指南_电子管功放怎么制作 电子管功放制作指南【详解】
  20. 怎么从主机拷贝文件到虚拟机

热门文章

  1. Sqlserver2014下载与安装
  2. Bootstrap模态框(modal)显示、隐藏与禁用ESC代码实现
  3. Python脚本打包成exe文件
  4. post install error,please remove node_moules before retry
  5. 如何重置云服务器系统
  6. springboot启动不了_七款高Star的开源SpringBoot扩展,助你的代码水平更上一层楼
  7. C#学习小结(DAY1)
  8. 直播预告丨基于 SDAF 闭环制胜 2022 数字化运营
  9. 第二届「星斗奖」获奖名单正式公布
  10. 插件编写傻瓜教程VC6.0