我想在我的UINavigationBar中有一个titleView,它有两行文本而不是一行
当我有一个“后退”按钮和一个“编辑”按钮,因为它看起来几乎居中,我目前的实现工作很好.问题是当我只有一个按钮.看起来真的很奇怪,因为这个视图可以在可用的空间上居中.@H_301_3@
UINavigationBar with left and right Button@H_301_3@
UINavigationBar with only one Button@H_301_3@
这是我目前的实现:@H_301_3@
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:titleLabel]; 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.
– 容器视图的宽度设置两个标签的最大宽度
– 将视图中的两个标签中较小的一个居中
– 为您的两个标签中的每一个使用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: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变体:@H_301_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;