[英]How do I place an image next to a title in a navigation bar controller?
I think the first step would be to add an image view. 我认为第一步是添加图像视图。 If I have to add any code, please tell me where it belongs.
如果我必须添加任何代码,请告诉我它属于哪里。 I'm not asking you to go out of your way to tell me how to add the code.
我并不是要您尽力告诉我如何添加代码。 I know how to do that.
我知道该怎么做。 I just want to know where in "UIViewController.h" the code belongs.
我只想知道代码在“ UIViewController.h”中的位置。 Oh, and if the image has to be a PNG, please tell me.
哦,如果图片必须是PNG,请告诉我。
Add this code into your viewDidLoad
: 将此代码添加到您的
viewDidLoad
:
UIImage *image = [UIImage imageNamed: @"logo_icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(yourXposition, yourYposition, yourWidth, yourHeight);
[self.navigationController.navigationBar addSubview:imageView];
The following code would put an image where the title goes. 以下代码会将图像放在标题所在的位置。 If you want both, you'll need a UIView that has both the image and a label for title added to it, and then set that UIView as the view in the second line of code below (NOTE: If using ARC remove the autorelease part):
如果两者都需要,则需要一个同时添加了图像和标题标签的UIView,然后在下面的第二行代码中将该UIView设置为视图(注意:如果使用ARC,请删除自动释放部分):
UIImage *image = [UIImage imageNamed:@"image.png"];
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.