UINavigationBar TitleView with subtitle

后端 未结 9 853
一生所求
一生所求 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

    I did this by completely replacing the title view. The end result looks like this:

    It can be achieved with the following code in the viewDidLoad method.

    UIView * containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 40)];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"Ttitle";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:titleLabel.font.pointSize];
    
    [containerView addSubview:titleLabel];
    titleLabel.keepInsets.equal = 0;
    titleLabel.keepBottomInset.equal = 10;
    
    UILabel * subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.textAlignment = NSTextAlignmentCenter;
    subtitleLabel.font = [UIFont italicSystemFontOfSize:12.0];
    subtitleLabel.textColor = [UIColor lightGrayColor];
    
    [containerView addSubview:subtitleLabel];
    
    subtitleLabel.keepHeight.equal = 15;
    subtitleLabel.keepWidth.equal = 200;
    subtitleLabel.keepBottomInset.equal = 0;
    subtitleLabel.keepHorizontalCenter.equal = 0.5;
    
    subtitleLabel.text = @"Subtitle";
    
    [self.navigationItem setTitleView:containerView];
    

    To do this I used the excellent KeepLayout library. It yields a result like this:

提交回复
热议问题