UINavigationBar TitleView with subtitle

后端 未结 9 868
一生所求
一生所求 2021-01-01 21:09

I want a titleView inside my UINavigationBar which has two lines of text instead of only one

My current implementiation works well when I have a \"Back\"-Button and

9条回答
  •  一向
    一向 (楼主)
    2021-01-01 21:40

    Inspired by @Ben Smiley, I added a masonry implementation.

    CGFloat width = 200;
    CGFloat titleHeight = 25;
    CGFloat containerHeight = 40;
    
    UIView * containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, containerHeight)];
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"Title";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:titleLabel.font.pointSize];
    
    [containerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(titleHeight);
    
    }];
    
    UILabel * subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.textAlignment = NSTextAlignmentCenter;
    subtitleLabel.font = [UIFont systemFontOfSize:12];
    
    [containerView addSubview:subtitleLabel];
    
    [subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(titleLabel.mas_bottom);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(containerHeight - titleHeight);
    
    }];
    
    subtitleLabel.text = @"Subtitle";
    
    [self.navigationItem setTitleView:containerView];
    

提交回复
热议问题