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

有这样一个需求,就是一个页面的顶部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留的位置

问题解决了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值