Skip to content

Instantly share code, notes, and snippets.

@wanghengheng
Last active December 28, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wanghengheng/0c422e4d72f0aa805317c2614358e527 to your computer and use it in GitHub Desktop.
Save wanghengheng/0c422e4d72f0aa805317c2614358e527 to your computer and use it in GitHub Desktop.
iOS - Navigation 设置副标题
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = kColorNavTitle;
titleLabel.font = [UIFont boldSystemFontOfSize:17];
titleLabel.text = [[_myCodeTree.path componentsSeparatedByString:@"/"] lastObject];
[titleLabel sizeToFit];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 0, 0)];
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = kColorDark7;
subTitleLabel.font = [UIFont systemFontOfSize:12];
subTitleLabel.text = _myCodeTree.path;
[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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment