Sin*_*hia 2 iphone xcode uinavigationbar
我在xib中添加了导航栏和导航项.现在我需要在导航栏的左侧添加图像,但它没有被添加.这是我正在处理的代码:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么图像没有被添加.我该如何添加它?
看起来这是一个老问题,接受了答案.但是,我想补充一下:
至少在Xcode 5中,如果您使用的是故事板,则可以通过将空视图拖动到标题区域,然后将ImageView放在空视图中,将图像添加为navigationBar的标题.
以下屏幕截图可以让您了解它的外观:

@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect {
CGRect currentRect = CGRectMake(0,0,100,45);
UIImage *image = [UIImage imageNamed:@"ic_launcher.png"];
[image drawInRect:currentRect];
}
@end
Run Code Online (Sandbox Code Playgroud)
UPDATE
只需在您的viewDidLoad:方法中添加此代码..
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
Run Code Online (Sandbox Code Playgroud)
如果你想在NavigationBar的背景中直接设置图像,那么使用波纹线...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
也看到这个链接如何添加一个定制的图像按钮到uinavigationbar-in-iphone-sdk
我希望这能帮到你......