本章实现效果:

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 设置导航栏

    //设置导航栏的标题 self.navigationItem setTitle:@"我的标题"; //设置导航条标题属性:字体大小/字体颜色…… /*设置头的属性:setTitle ...

  2. IOS 设置导航栏全局样式

    // 1.设置导航栏背景 UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgroundImage:[UIImage r ...

  3. iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)

                      #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicati ...

  4. iOS 设置导航栏的颜色和导航栏上文字的颜色

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  5. iOS设置导航栏样式(UINavigationController)

    //设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [bar ...

  6. iOS 设置导航栏全透明

    - (void)viewWillAppear:(BOOL)animated{ //设置导航栏背景图片为一个空的image,这样就透明了 [self.navigationController.navig ...

  7. ios 设置导航栏背景色

    //设置导航栏背景色 如果上面的不好用 就用下面的 [self.navigationController.navigationBar setBackgroundImage:[UIImage image ...

  8. IOS设置导航栏字体大小及颜色

    方法一: 自定义视图,定义一个lable,相关属性在lable里设置 核心方法: self.navigationItem.titleView = titleLabel; 方法二:用系统方法直接设置 [ ...

  9. iOS设置导航栏标题

    方法一:在UIViewController中设置self.title. 方法二:设置self.navigationItem.titleView.

随机推荐

  1. SQL SERVER-数据库的远程访问解决办法

    除了下面的这个RemoteDacEnabled更改为true之后,还要把防火墙关闭,才能通过IP地址访问数据库 来自为知笔记(Wiz)

  2. ASP.NET-HTTP响应标头

    Reponse Headers 理论上所有的响应头信息都应该是回应请求头的.但是服务端为了效率,安全,还有其他方面的考虑,会添加相对应的响应头信息,从上图可以看到: Cache-Control:mus ...

  3. Android View 事件分发机制 源代码解析 (上)

    一直想写事件分发机制的文章,无论咋样,也得自己研究下事件分发的源代码.写出心得~ 首先我们先写个简单的样例来測试View的事件转发的流程~ 1.案例 为了更好的研究View的事件转发,我们自定以一个M ...

  4. bzoj3442: 学习小组(费用流好题)

    3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...

  5. ThinkPHP5.0框架开发--第6章 TP5.0 请求和响应

    ThinkPHP5.0框架开发--第6章 TP5.0 请求和响应 第6章 TP5.0 请求和响应 ===================================== 上次复习 1.新建控制器 ...

  6. zzulioj--1609--求和(数学规律)

     1609: 求和 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 209  Solved: 67 SubmitStatusWeb Board De ...

  7. web语义化理解

    含义: Web语义化是指使用语义恰当的标签,使页面有良好的结构,页面元素有含义,能够让人和搜索引擎都容易理解. 为什么要web语义化?如今互联网都到了web2.0的时代了,HTML语言在不断的进化并发 ...

  8. (转载) Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题

    Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题 标签: scrollviewandroid滑动嵌套 2015-07-16 17:24 1112 ...

  9. [ Docker ] 映射資料夾

    - docker run -v <host path>:<container path> - 例如:docker run -v /home/adrian/data:/data ...

  10. Ubuntu 14.04下安装CUDA8.0

    配置环境如下: 系统:Ubuntu14.04 64bit 显卡:Nvidia K620M 显卡驱动:Nvidia-Linux-x86_64-375.66.run CUDA8.0 +  cudnn8.0 ...