本章实现效果:

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;

最新文章

  1. asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(三)
  2. python——有一种线程池叫做自己写的线程池
  3. asp.net mvc通过预处理实现数据过滤和数据篡改。
  4. 每日一九度之 题目1043:Day of Week
  5. Java troubleshooting guide
  6. bzoj4518
  7. 在Fragment中使用BaiduMap SDK
  8. Android Java混淆(ProGuard)
  9. PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different
  10. TableLayout中怎么填充相同的布局
  11. VB中的GDI编程-2 画笔
  12. Linux常用命令手册
  13. (六)java结构控制语句
  14. linux 下搭建php环境
  15. Linux系统Vi/Vim编辑器的简单介绍、安装/卸载、常用命令
  16. SQL Server-聚焦深入理解死锁以及避免死锁建议(转载)
  17. input的placeholder在ie9下不兼容的结局办法。
  18. 【Azure】Publish Error of &quot;%(TargetOSFamily.Identity)&quot; that evaluates to &quot;&quot; instead of a number
  19. python之插入排序
  20. 每日英语:China Poses Challenge for Coal

热门文章

  1. 极路由4pro交叉编译c、c++的代码
  2. flex 通过htmlservices链接moss的rest(rest 的get post方式)
  3. 轻松学习JavaScript十七:JavaScript的BOM学习(二)
  4. FireFox所支持的全部标签(持续更新ing)
  5. bzoj4547: Hdu5171 小奇的集合(矩阵乘法)
  6. ThinkPHP5.0框架开发--第1章 Tp5.0安装
  7. Sql Server创建外键失败
  8. [ Linux ] [ OS ] [ CPU ] Linux系統 OS, CPU, Memory, Disk
  9. vue.js---methods中一个方法调用另一个方法
  10. 3ds Max绘制青花瓷茶壶