如何在UINavigationController标题中添加tile旁边的图标?

new*_*bie 3 objective-c uinavigationbar uinavigationcontroller ios ios7

我正在尝试在导航栏标题旁边添加一个下拉箭头或图标(如下面的屏幕截图所示)但是没有找到一个好的解决方案,我认为这将是相当直接但我无法得到一个很好的解决方案.

我尝试过的一种方法是UIButton通过设置视图控制器来替换标题,navigationItem.titleView但这种方法的问题是因为我的标题长度可能不同我不能计算的按钮帧大小CGRect报告为0,0.如果我尝试在viewWillDisplay()方法中更新按钮的框架,那么按钮框架更改将放大动画到位并且对用户可见并且非常刺耳的效果.

有没有其他可能的解决方案,我觉得我只是接近这一切都错了,应该不是这么难.

带箭头的导航栏

Ash*_*kad 9

如果您的角色在列表中以Unicode字符的形式提供,则可以将其添加到字符串中

按:Ctrl + Command + Space

在此输入图像描述

假设你的角色是DOWN ARROWHEAD,那么你可以将它添加到你的标题字符串中.

如果你想添加属性字符串或图标,那么你可以按照Leo的答案.


Leo*_*Leo 7

您可以使用attributionstring设置标签,它会自动调整大小

简称

在此输入图像描述

长标题

在此输入图像描述

例如

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,100, 40)];
label.textAlignment = NSTextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"memoAccess"];
attach.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Title"];
[mutableAttriStr appendAttributedString:imageStr];
label.attributedText = mutableAttriStr;
self.navigationItem.titleView = label;
Run Code Online (Sandbox Code Playgroud)