Last active
December 28, 2018 08:50
-
-
Save wanghengheng/0c422e4d72f0aa805317c2614358e527 to your computer and use it in GitHub Desktop.
iOS - Navigation 设置副标题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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