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
Ios 将UIImage或UILabel添加到navigationItems_Ios_Uilabel_Uinavigationbar_Uibarbuttonitem_Uinavigationitem - Fatal编程技术网

Ios 将UIImage或UILabel添加到navigationItems

Ios 将UIImage或UILabel添加到navigationItems,ios,uilabel,uinavigationbar,uibarbuttonitem,uinavigationitem,Ios,Uilabel,Uinavigationbar,Uibarbuttonitem,Uinavigationitem,在我的应用程序中,我必须根据我所在的页面自定义导航栏。在某些视图中,我需要添加一些ui按钮,但也需要添加一些ui图像(用于未居中的徽标)或一些ui标签 我可以使用[self.navigationController.navigationBar addSubview:myLabel]来实现这一点。但我并不喜欢这种方法,因为如果我必须添加ui按钮,或者只是更改ui标签的文本,我需要手动更改每个ui标签和每个ui图像的原点 我想用一些已经存在的东西:uibarbuttoneim。它可以包含一些文本和图

在我的应用程序中,我必须根据我所在的页面自定义导航栏。在某些视图中,我需要添加一些
ui按钮
,但也需要添加一些
ui图像
(用于未居中的徽标)或一些
ui标签

我可以使用
[self.navigationController.navigationBar addSubview:myLabel]
来实现这一点。但我并不喜欢这种方法,因为如果我必须添加
ui按钮
,或者只是更改
ui标签
的文本,我需要手动更改每个
ui标签
和每个
ui图像
的原点

我想用一些已经存在的东西:
uibarbuttoneim
。它可以包含一些文本和图像,我只需要将它们添加到
导航项中

剩下的唯一问题是,它仍然是一个按钮,按下时会像按钮一样反应。如果我禁用它,它的alpha变化显示它被禁用。。。我怎么能保持它的正常外观,让它失效呢

谢谢


PS:我愿意接受在导航栏中添加UIImages或UILabel的任何其他想法:)

试试这个代码片段。您可以根据需要更改标签和imageView框架并放置

UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = @"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = @"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];


UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
 imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:@"abc.png"];
 [btn addSubview:imgviw];

self.navigationItem.titleView = btn;

希望它能有所帮助。

试试这个代码片段。您可以根据需要更改标签和imageView框架并放置

UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];

UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = @"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = @"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];


UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
 imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:@"abc.png"];
 [btn addSubview:imgviw];

self.navigationItem.titleView = btn;

希望对您有所帮助。

您可以使用customView创建的barButton

比如说,

UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
[btn setTitle:@"some Title" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor greenColor]];

self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:btn];

您可以使用使用customView创建的按钮

比如说,

UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
[btn setTitle:@"some Title" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor greenColor]];

self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:btn];

您可以通过不为已创建按钮设置任何@selector,并为UIControlStateNormal和UIControlStateSelected设置相同的自定义属性来完成此操作。另外,将“染色颜色”设置为“透明颜色”。就像那样,按钮仍然是一个按钮,它会留在那里,看起来总是一样的,但它什么都不会做


希望有帮助。

您可以通过不为已创建按钮设置任何@selector,并为UIControlStateNormal和UIControlStateSelected设置相同的自定义属性来做到这一点。另外,将“染色颜色”设置为“透明颜色”。就像那样,按钮仍然是一个按钮,它会留在那里,看起来总是一样的,但它什么都不会做


希望能有所帮助。

这个答案与@santhu的答案相结合,为我提供了解决方案。谢谢:)这个答案加上@santhu的一个给了我答案。谢谢:)