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

前端之家收集整理的这篇文章主要介绍了ios – 如何将图像和标签添加到UINavigationBar作为标题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前我只知道如何将字符串作为标题
[self setTitle:@"iOS CONTROL"];

或带图像

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

但不是两个在同一时间

解决方案吗?谢谢

解决方法

您可以创建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;
原文链接:https://www.f2er.com/iOS/332760.html

猜你在找的iOS相关文章