问题描述
我知道我可以在导航栏中将 UIImageView
设置为 titleView
属性,但是有什么方法可以让它使用图像加上 self.title
?我想在应用程序中使用一个小徽标,但仍保留显示它们所在位置的文本。到目前为止,我唯一能做的就是为每个单独的视图添加单词,并为每个视图添加不同的图像,但这根本行不通,因为每个图像的大小都会增加,它只是一个很多工作。
我试过了:
UIImage *headerIcon = [UIImage imageNamed:@"navbarlogo"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = headerIcon;
NSAttributedString *icon = [NSAttributedString attributedStringWithAttachment:textAttachment];
// space between icon and title
NSAttributedString *space = [[NSAttributedString alloc] initWithString:@" "];
// Title
NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"Home"];
// new title
NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithAttributedString:icon];
[attributedTitle appendAttributedString:space];
[attributedTitle appendAttributedString:title];
// move text up to align with image
[attributedTitle addAttribute:NSBaselineOffsetAttributeName
value:@(10.0)
range:NSMakeRange(1,attributedTitle.length-1)];
UILabel *titleLabel = [UILabel new];
titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17.f];
titleLabel.attributedText = attributedTitle;
[titleLabel sizetoFit];
self.navigationItem.titleView = titleLabel;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)