本章实现效果:

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. angular-代码段

    重复代码 <div ng-app="" ng-init="names=['Jani','Hege','Kai']"> <p>使用 ng- ...

  2. sqlite学习笔记9:C语言中使用sqlite之插入数据

    前面创建了一张表,如今给他插入一些数据.插入数据跟创建表差点儿相同,不过SQL语言不一样而已,完整代码例如以下: #include <stdio.h> #include <stdli ...

  3. bzoj1103: [POI2007]大都市meg(树链剖分)

    1103: [POI2007]大都市meg 题目:传送门 简要题意: 给你一棵树,给出每条边的权值,两个操作:1.询问根到编号x的最短路径的权值和  2.修改一条边的边权 题解: 很明显啊,看懂了题基 ...

  4. Forms authentication timeout vs sessionState timeout

    https://stackoverflow.com/questions/17812994/forms-authentication-timeout-vs-sessionstate-timeout Th ...

  5. zzulioj--1716--毒(模拟水题)

     1716: 毒 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 96  Solved: 43 SubmitStatusWeb Board Desc ...

  6. OEM12C(12.1.0.5)安装插件监控mysql(linux)

    目录结构: 文章参考论坛:https://blog.csdn.net/u010719917/article/details/78128200 环境说明: oms:12.1.0.5  os:centos ...

  7. Uncaught TypeError: undefined is not a function

    index.html <script src="resources/sap-ui-core.js" id="sap-ui-bootstrap" data- ...

  8. POJ 2481 Cows【树状数组】

    题意:给出n头牛的s,e 如果有两头牛,现在si <= sj && ei >= ej 那么称牛i比牛j强壮 然后问每头牛都有几头牛比它强壮 先按照s从小到大排序,然后用e来 ...

  9. 线段树(segment tree )

    http://www.cnblogs.com/TenosDoIt/p/3453089.html 写的非常好! 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很 ...

  10. 搭建javawebxiangmu

    https://blog.csdn.net/qq_23994787/article/details/73612870#