iPhone-SDK:在导航栏上添加图像: [英] iPhone-SDK: Adding Image on Navigation Bar:

查看:71
本文介绍了iPhone-SDK:在导航栏上添加图像:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航控制器,它也有一个表格视图.我想以编程方式在导航栏的左侧添加一个图像.当我尝试在viewDidLoad中添加以下代码时:

I have a navigation controller which also has a table view. I want to add an image left side of the navigation bar on top programmatically. When i tried to add by the below code in viewDidLoad:

CGRect frame = CGRectMake(0.0, 0.0, 94.0, 33.0);
    UIImageView *image = [ [UIImageView alloc]initWithImage:[UIImage imageNamed:@"topBarImage.png"] ];
    image.frame = frame;
    [self.view addSubview:image];

,但是它将添加到表格视图的顶部,而不是导航选项卡.我认为,该图像应添加为navigationItem.但是我不怎么以编程方式在导航栏的左侧添加图像.能请一位向导吗?

but it is adding to top of the table view instead of navigation tab. I think, this image should be added as navigationItem. But i don't how to add an image on left side of navigation bar programmatically. Could some one guide please?

Clave/

推荐答案

您仅将自定义视图用于条形按钮项目.这是一个带有自定义图像按钮的示例

you just use a custom view for a bar button item. Here is an example of one with a custom image button

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(blah) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

您要这样做,因为导航控制器的标题将以这种方式自动调整大小.如果您按照自己的方式操作(导航栏的子视图),则文本将在图像的后面或上面

You want to do it this way because the title of the nav controller will be auto resized this way. If you do it your way (subview of the nav bar) the text will go behind or ontop of the image

当您弹出并按下视图控制器时,它也将保留在屏幕上.

edit: it will also stay on the screen as you pop and push view controllers.

edit2:再次,您可以将imageview用作自定义视图,而不是按钮.这是代码

edit2: once again, you can use an imageview as the custom view and not the button. Here is code

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setImage:[UIImage imageNamed:@"myImage.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:iv];
[iv release];

这篇关于iPhone-SDK:在导航栏上添加图像:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆