Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UINavigationController标题中的平铺旁边添加图标?_Ios_Objective C_Ios7_Uinavigationcontroller_Uinavigationbar - Fatal编程技术网

Ios 如何在UINavigationController标题中的平铺旁边添加图标?

Ios 如何在UINavigationController标题中的平铺旁边添加图标?,ios,objective-c,ios7,uinavigationcontroller,uinavigationbar,Ios,Objective C,Ios7,Uinavigationcontroller,Uinavigationbar,我正在尝试在导航栏标题旁边添加一个下拉箭头或图标(如下面的屏幕截图所示),但还没有找到一个好的解决方案,我认为这会很简单,但我就是找不到一个好的解决方案 我尝试过的一种方法是通过设置视图控制器的navigationItem将标题替换为UIButton。titleView,但这种方法的问题是,由于标题的长度可能会有所不同,我无法计算按钮帧大小,因此cRect报告为0,0。如果我尝试在viewWillDisplay()方法中更新按钮的帧,那么按钮帧的更改将放大到动画中,用户可以看到,并且效果相当不和

我正在尝试在导航栏标题旁边添加一个下拉箭头或图标(如下面的屏幕截图所示),但还没有找到一个好的解决方案,我认为这会很简单,但我就是找不到一个好的解决方案

我尝试过的一种方法是通过设置视图控制器的
navigationItem将标题替换为
UIButton
。titleView
,但这种方法的问题是,由于标题的长度可能会有所不同,我无法计算按钮帧大小,因此
cRect
报告为0,0。如果我尝试在
viewWillDisplay()
方法中更新按钮的帧,那么按钮帧的更改将放大到动画中,用户可以看到,并且效果相当不和谐

有没有其他可能的解决办法,我觉得我只是走错路了,不应该这么难


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

简称

长标题

比如说

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;

如果您的字符在列表中可用作Unicode字符,则可以将其添加到字符串中

按:Ctrl+Command+Space

假设您的角色是向下箭头,则可以将其添加到标题字符串中


如果要添加属性化字符串或图标,则可以按照的答案进行操作。

创建包含UILabel和UIButton的UIView

UIView * container = [[UIView alloc]initWithFrame:CGRectZero];
container.backgroundColor = [UIColor clearColor];

// title text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
label.backgroundColor = [UIColor clearColor];
label.text = @"hello world";
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.adjustsFontSizeToFitWidth = YES;
label.lineBreakMode = NSLineBreakByClipping;
label.numberOfLines = 1;
[label sizeToFit];

// button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(label.frame.origin.x, label.frame.size.width, 20, 40);

// resize container
container.frame = CGRectMake(0, 0, label.frame.size.width + button.frame.size.width, 40);

[container addSubview:label];
[container addSubview:button];

self.navigationItem.titleView = container;

您知道是否可以设置图标图像更改的动画,例如通过设置CATTransferm3dMakeRotation等?我希望标签上的点击手势将向下的V形旋转到向上的V形。您是如何发现标签框rect为0,0100,40的?如果您不知道大小怎么办?很酷,如果unicode字符与标签的字体对齐,这是一个很好的简单解决方案。我之前尝试过这个,但不喜欢unicode图标。这不起作用,因为label.frame.size在viewWillDisplay()之前将为0,0