如何将图像和标签添加到UINavigationBar作为标题?

时间:2014-03-04 12:00:12

标签: ios uiimage uinavigationbar

目前我只知道如何将字符串作为标题:

[self setTitle:@"iOS CONTROL"];

或带

的图片
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

但不是两者同时

有解决方案吗?谢谢

2 个答案:

答案 0 :(得分:16)

您可以创建UIView并添加任意数量的子视图。例如:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 40.0)];
titleView.backgroundColor = [UIColor clearColor];

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor redColor];
titleLabel.text = @"My Title Label";
[titleLabel sizeToFit];

...

// Set the frames for imageView and titleLabel to whatever you need them to be

...

[titleView addSubview:imageView];
[titleView addSubview:titleLabel];

self.navigationItem.titleView = titleView;

答案 1 :(得分:1)

以您希望的方式创建新的UIView,添加UIImageUILabel作为子视图,并使用UIView添加self.navigationItem.titleView