我想在我的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: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变体:
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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。