UINavigationBar TitleView with subtitle

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

    Here is how I would do it:

    • Use sizeToFit for each of your two labels.
    • Have the container view's width set to the max width of your two labels.
    • Center the smaller of the two labels on the view.

    This code should work for you:

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:20];
        titleLabel.text = @"Your Title";
        [titleLabel sizeToFit];
    
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 0, 0)];
        subTitleLabel.backgroundColor = [UIColor clearColor];
        subTitleLabel.textColor = [UIColor whiteColor];
        subTitleLabel.font = [UIFont systemFontOfSize:12];
        subTitleLabel.text = @"Your subtitle";
        [subTitleLabel sizeToFit];
    
        UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(subTitleLabel.frame.size.width, titleLabel.frame.size.width), 30)];
        [twoLineTitleView addSubview:titleLabel];
        [twoLineTitleView addSubview:subTitleLabel];
    
        float widthDiff = subTitleLabel.frame.size.width - titleLabel.frame.size.width;
    
        if (widthDiff > 0) {
            CGRect frame = titleLabel.frame;
            frame.origin.x = widthDiff / 2;
            titleLabel.frame = CGRectIntegral(frame);
        }else{
            CGRect frame = subTitleLabel.frame;
            frame.origin.x = abs(widthDiff) / 2;
            subTitleLabel.frame = CGRectIntegral(frame);
        }
    
        self.navigationItem.titleView = twoLineTitleView;
    

    Swift 3 variant:

        let titleLabel = UILabel.init(frame: CGRect.zero)
        titleLabel.backgroundColor = UIColor.clear
        titleLabel.textColor = UIColor.schBlack
        titleLabel.font = UIFont.schTextStyle2Font()
        titleLabel.text = titleString;
        titleLabel.sizeToFit()
    
        let subTitleLabel = UILabel.init(frame: CGRect.init(x: 0, y: 22, width: 0, height: 0))
        subTitleLabel.backgroundColor = UIColor.clear
        subTitleLabel.textColor = UIColor.schBrownishGrey
        subTitleLabel.font = UIFont.schTextStyle3Font()
        subTitleLabel.text = subTitleString;
        subTitleLabel.sizeToFit()
    
        let twoLineTitleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.width-40, height: 30))
        twoLineTitleView.addSubview(titleLabel)
        twoLineTitleView.addSubview(subTitleLabel)
    
        /*
    
        float widthDiff = subTitleLabel.frame.size.width - titleLabel.frame.size.width;
        if (widthDiff > 0) {
            CGRect frame = titleLabel.frame;
            frame.origin.x = widthDiff / 2;
            titleLabel.frame = CGRectIntegral(frame);
        }else{
            CGRect frame = subTitleLabel.frame;
            frame.origin.x = abs(widthDiff) / 2;
            subTitleLabel.frame = CGRectIntegral(frame);
        }
        */
    
        self.navigationItem.titleView = twoLineTitleView;
    

提交回复
热议问题