本章实现效果:

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. UVALive 2664 One-way traffic

    One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...

  2. Sublime Text 3 Package Control安装

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50618314 安装好Sublime T ...

  3. 五大最佳开源java性能监控工具

    如果你正在寻找性能监控工具,不妨看看以下推荐的这五款开源工具,这些工具目前已经可以替代付费工具了,你可以看看是否是你的最佳选择.本文推荐的五款开源工具目前是开源社区中最受欢迎的. 1. Stagemo ...

  4. [using_microsoft_infopath_2010]Chapter2 表单需求,使用表决矩阵

    本章概要 1.从模板创建表单 2.从创建表单收集需求 3.使用全部表单决策 4.决定需要创建哪种表单

  5. HDU 2643

    (第二类斯特林数*N的阶乘 )的和. #include <iostream> #include <cstdio> #include <algorithm> #def ...

  6. Hive编程指南_学习笔记01

    第四章: HQl的数据定义 1:创建数据库   create database financials;   create database  if not exists financials; 2: ...

  7. IBM AppScan官方帮助文档错别字缺陷,IBM的測试人员也太粗心了吧

    袁术=元素?

  8. ES6 | 关于class类 继承总结

    子类必须在constructor方法中调用super方法,否则新建实例时会报错.这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工.如果不调用super方法,子类就得不到 ...

  9. kindEditor编写插件遇到的问题

    kindEditor是一个功能强大的在线文本编辑器,而且提供了插件扩展功能,更好的满足用户各方面的需求.在项目中,我们就有如此的需求:在kindEditor编辑器中,添加一条下划线,并且在下划线的中间 ...

  10. cuda windows运行时间限制

    GTX1080的卡,8G的显存,写cuda时申请了2G的显存就莫名其妙的抛异常,昨天找了一天也没找出bug,去stackoverflow上问了下才知道是windows的事,需要修改注册表.问题链接 - ...