您的位置:首页 > 产品设计 > UI/UE

iOS - UINavigationItem 的titleView显示不出来的解决方案

2017-12-14 14:08 399 查看
有这样一个需求,就是一个页面的顶部title需要是两行,上边是产品名称,下边是产品code,并且名称和code是不同的字号

查看UINavigationItem的关于title的接口和属性:



其中,title已经不能满足我的需求了,就用titleView

开始我的布局是用masonry布局的,制作了一个容器view,其中放了两个UIlabel,用masonry布局,但是容器view并没有布局,在iPhoneX的模拟器上是可以的,显示都征程,但是在真机上显示不出来,于是我做了如下:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-80, 44)];
titleView.backgroundColor = [UIColor clearColor];

UILabel *topLabel = [[UILabel alloc] init];
topLabel.text = self.navigationItem.title;
topLabel.font = kFT4;
topLabel.textColor = kCL1;
topLabel.numberOfLines = 1;

UILabel *bottomLabel = [[UILabel alloc] init];
bottomLabel.font = kFT0;
bottomLabel.textColor = kCL1;
bottomLabel.text = self.code;

[titleView addSubview:topLabel];
[titleView addSubview:bottomLabel];

[topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(titleView);
make.top.equalTo(titleView).offset(3);
}];
[bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(titleView);
make.top.equalTo(topLabel.mas_bottom);
make.bottom.equalTo(titleView).offset(-3);
}];

self.navigationItem.titleView = titleView;
关键是:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-80, 44)];


其中减去的80,是给UINavigationItem的left和right留的位置

问题解决了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS titleView 不显示