HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UINavigationBar TitleView带字幕大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的UINavigationBar中有一个titleView,它有两行文本而不是一行

我有一个“后退”按钮和一个“编辑”按钮,因为它看起来几乎居中,我目前的实现工作很好.问题是当我只有一个按钮.看起来真的很奇怪,因为这个视图可以在可用的空间上居中.

UINavigationBar with left and right Button

UINavigationBar with only one Button

这是我目前的实现:

CGRect frame = self.navigationController.navigationBar.frame;

UIView *twoLinetitleView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(framE),CGRectGetWidth(framE),CGRectGetHeight(framE))];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,8,14)];
titleLabel.BACkgroundColor = [UIColor clearColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = self.title;
[twoLinetitleView addSubview@R_857_6964@Label];

UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,23,14)];
subtitleLabel.BACkgroundColor = [UIColor clearColor];
subtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
subtitleLabel.textAlignment = UITextAlignmentCenter;
subtitleLabel.text = self.subtitle;
[twoLinetitleView addSubview:subtitleLabel];

self.navigationItem.titleView = twoLinetitleView;

解决方法

这是我将如何做:
– 为您的两个标签中的每一个使用sizeToFit.
– 容器视图的宽度设置两个标签的最大宽度
– 将视图中的两个标签中较小的一个居中

这个代码应该适合你:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(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)];
    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,MAX(subtitleLabel.frame.size.width,titleLabel.frame.size.width),30)];
    [twoLinetitleView addSubview@R_857_6964@Label];
    [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变体:

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;

大佬总结

以上是大佬教程为你收集整理的ios – UINavigationBar TitleView带字幕全部内容,希望文章能够帮你解决ios – UINavigationBar TitleView带字幕所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。