Iphone/Ipad 将图像和文本添加到导航标题

发布于 2024-10-06 17:49:32 字数 280 浏览 8 评论 0原文

我只是想将图像和文本添加到 navigationItem 标题。这就是我正在做的,但它只显示图像而不显示标题。这看起来似乎很容易。

例子 (

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_small_white.png"]] autorelease];
self.navigationItem.title = @"Company";

I am simply trying to add an image and text to the navigationItem title. This is what I am doing, but it only shows the image and not the title. This seems like it would be pretty easy.

Example (

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_small_white.png"]] autorelease];
self.navigationItem.title = @"Company";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲜血染红嫁衣 2024-10-13 17:49:32

这是我的代码:

UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)]; 
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(40, 0, 300, 30)];

title.text = NSLocalizedString(@"My Title", nil);
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont boldSystemFontOfSize:20.0]];

[title setBackgroundColor:[UIColor clearColor]];
UIImage *image = [UIImage imageNamed:@"MyLogo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];

myImageView.frame = CGRectMake(0, 0, 30, 30); 
myImageView.layer.cornerRadius = 5.0;
myImageView.layer.masksToBounds = YES;
myImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
myImageView.layer.borderWidth = 0.1;

[myView addSubview:title];
[myView setBackgroundColor:[UIColor  clearColor]];
[myView addSubview:myImageView];
self.navigationItem.titleView = myView;

Here is my code:

UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)]; 
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(40, 0, 300, 30)];

title.text = NSLocalizedString(@"My Title", nil);
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont boldSystemFontOfSize:20.0]];

[title setBackgroundColor:[UIColor clearColor]];
UIImage *image = [UIImage imageNamed:@"MyLogo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];

myImageView.frame = CGRectMake(0, 0, 30, 30); 
myImageView.layer.cornerRadius = 5.0;
myImageView.layer.masksToBounds = YES;
myImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
myImageView.layer.borderWidth = 0.1;

[myView addSubview:title];
[myView setBackgroundColor:[UIColor  clearColor]];
[myView addSubview:myImageView];
self.navigationItem.titleView = myView;
那小子欠揍 2024-10-13 17:49:32

通过设置自定义标题视图,您将替换通常显示标题文本的标准视图。来自 UINavigationItemtitleView 的文档:

如果将此属性设置为自定义标题,则会显示该属性而不是标题。

因此,您需要添加自己的视图(可能是 UILabel)来显示标题 - 一种方法是添加 UILabelUIImageView< /code> 作为容器 UIView 的子视图,并将 that 设置为 titleView

By setting a custom title view, you're replacing the standard view that normally shows the title text. From the documentation for titleView in UINavigationItem:

If you set this property to a custom title, it is displayed instead of the title.

So you need to add your own view (possibly a UILabel) to display your title — one way of doing this would be to add a UILabel and the UIImageView above as subviews of a container UIView, and set that as the titleView.

人│生佛魔见 2024-10-13 17:49:32

在 Swift 2 中:

var myView: UIView = UIView(frame: CGRectMake(0, 0, 300, 30))
var title: UILabel = UILabel(frame: CGRectMake(40, 0, 300, 30))
title.text = "The Title"
title.textColor = UIColor.blackColor()
title.font = UIFont.boldSystemFontOfSize(20.0)
title.backgroundColor = UIColor.clearColor()
var image: UIImage = UIImage(named: "your-image")!
var myImageView: UIImageView = UIImageView(image: image)
myImageView.frame = CGRectMake(0, 0, 30, 30)
myImageView.layer.cornerRadius = 5.0
myImageView.layer.masksToBounds = true
myImageView.layer.borderColor = UIColor.lightGrayColor().CGColor
myImageView.layer.borderWidth = 0.1
myView.addSubview(title)
myView.backgroundColor = UIColor.clearColor()
myView.addSubview(myImageView)

self.navigationItem.titleView = myView

相关问题:titleView 自动居中,如果上面的示例不是这种情况,那么您可能使用较小的屏幕尺寸(例如 iPhone)。只需根据需要将 UIView 和 UILabel 的宽度从 300 更改为更小即可。当有空间时它会弹回到中心。

In Swift 2:

var myView: UIView = UIView(frame: CGRectMake(0, 0, 300, 30))
var title: UILabel = UILabel(frame: CGRectMake(40, 0, 300, 30))
title.text = "The Title"
title.textColor = UIColor.blackColor()
title.font = UIFont.boldSystemFontOfSize(20.0)
title.backgroundColor = UIColor.clearColor()
var image: UIImage = UIImage(named: "your-image")!
var myImageView: UIImageView = UIImageView(image: image)
myImageView.frame = CGRectMake(0, 0, 30, 30)
myImageView.layer.cornerRadius = 5.0
myImageView.layer.masksToBounds = true
myImageView.layer.borderColor = UIColor.lightGrayColor().CGColor
myImageView.layer.borderWidth = 0.1
myView.addSubview(title)
myView.backgroundColor = UIColor.clearColor()
myView.addSubview(myImageView)

self.navigationItem.titleView = myView

Related gotcha: titleView centres automatically, if this isn't the case with the above example then you're likely using a smaller screensize (eg. iPhone). Simply change the width of the UIView and UILabel from 300 to less, as suited. It'll snap back to centre when there is room.

绾颜 2024-10-13 17:49:32
UIImage *image = [UIImage imageNamed:@"image.png"]; // Image name with extension

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];

这可能是做到这一点的最短方法。

UIImage *image = [UIImage imageNamed:@"image.png"]; // Image name with extension

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];

This maybe the shortest way to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文