如何在 iPhone 应用程序的 UINavigationBar 中添加图像

2023-06-13移动开发问题
8

本文介绍了如何在 iPhone 应用程序的 UINavigationBar 中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在我的应用程序中,我想在导航栏中添加图像(徽标).我正在开发 XCode 4.2 和 iOS 5.

In my app, i want to add image(logo) in Navigation bar. I am working on XCode 4.2 and iOS 5.

我知道 UINavigationBarUIToolBar 在 iOS 5 中已更改.所以 iOS 4.2 UINavigationBar 代码在 iOS 5 中将无法使用.

I know UINavigationBar, UIToolBar has been changed in iOS 5. So iOS 4.2 UINavigationBar code won't work in iOS 5.

我希望在 4.2 和 5 版本中都支持 UINavigationBar 中的显示图像.

I want to support display image in UINavigationBar in both 4.2 and 5 version.

我想在 UINavigationBar 中显示图像,如下图所示.

I want to be display image in UINavigationBar like as in below screenshot.

请在这方面提供帮助,如果有任何示例代码意味着它对我很有帮助.

Please help in this regards and if there is any sample code means its very helpful to me.

谢谢!!!

推荐答案

一种方法是使用 UINavigationItem.titleViewUINavigationItem.rightBarButtonItem.像这样:

One way to do this is to use UINavigationItem.titleView and UINavigationItem.rightBarButtonItem. Like this :

viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
viewController.navigationItem.rightBarButtonItem = item;    

这里我使用 UIImageView 作为自定义视图,但它可以是带有自定义图像的 UIButton.

Here I am using UIImageView as custom view, but it can be UIButton with custom image.

这篇关于如何在 iPhone 应用程序的 UINavigationBar 中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7