简体   繁体   English

如何在iPhone应用程序的导航栏中显示图像?

[英]how to display an image in the navigation bar of an iPhone application?

how to display an image in the navigation bar of an iPhone application? 如何在iPhone应用程序的导航栏中显示图像? (say, right after the title) (比如,在标题之后)

Here's how to have the image centered in the NavBar. 以下是如何将图像置于NavBar中心。

UIImage *image = [UIImage imageNamed: @"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

self.navigationItem.titleView = imageView;

[imageView release];

This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple. 此代码实际上包含在NavBar的Apple源代码中,可以在Apple的iPhone开发人员中心找到。 The source shows you various ways to manipulate both the StatusBar & NavBar. 源显示了各种操作StatusBar和NavBar的方法。

I haven't tested this but as UINavigationBar is a view you can add subViews to it. 我没有测试过这个,但是因为UINavigationBar是一个视图,你可以添加子视图。

UIImage* myImage = [UIImage imageNamed:@"Myimage.png"];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;

[myTabbar addSubView:myImageView];
[myImageView release];

You can use things like the backItem property to calculate the position of your image view. 您可以使用backItem属性之类的内容来计算图像视图的位置。

If you want the image at the right of the nav bar, you can define it as a custom button with no action when presed, like this 如果您想要导航栏右侧的图像,您可以将其定义为自定义按钮,在预设时不执行任何操作,如下所示

UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];

Simply Place that code in - (void)viewWillAppear:(BOOL)animated; 只需将代码放入- (void)viewWillAppear:(BOOL)animated; so it'll work fine and add one image having size 320x40 named Top Bar 所以它会正常工作并添加一个名为Top Bar的尺寸为320x40的图像

UIImage *image = [UIImage imageNamed: @"TopBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

the navigation bar has a property called title view - set this to the image you like. 导航栏有一个名为标题视图的属性 - 将其设置为您喜欢的图像。 Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. 由于titleView会覆盖导航栏的标题,因此您必须在图像文件中包含所需的标题。 Still set the title to what you want so it appears on the back button when you push a view Controller 仍然将标题设置为您想要的标题,以便在按下视图控制器时它显示在后退按钮上

I encountered the same problem.Found out the best solution 我遇到了同样的问题。找出最佳解决方案

 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background_image.png"] forBarMetrics:UIBarMetricsDefault];

Hope this would help.... 希望这会有所帮助....

Just write your own navigation bar. 只需编写自己的导航栏。 Therefore you have to disable the Navigation Bar fist: 因此,您必须禁用导航栏拳头:

Disable the top bar in the interface builder by selecting your Navigation Controller in your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None 通过在故事板中选择导航控制器来禁用界面构建器中的顶部栏:属性检查器 - >模拟度量标准 - >顶栏:选择无

Afterwards you can add any HeaderView you like... 之后你可以添加你喜欢的任何HeaderView ......

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"header_image.png"]];
self.headerView.backgroundColor = background;

// ...add buttons and labels

[self.view addSubview:headerView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM