繁体   English   中英

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

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

如何在iPhone应用程序的导航栏中显示图像? (比如,在标题之后)

以下是如何将图像置于NavBar中心。

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

self.navigationItem.titleView = imageView;

[imageView release];

此代码实际上包含在NavBar的Apple源代码中,可以在Apple的iPhone开发人员中心找到。 源显示了各种操作StatusBar和NavBar的方法。

我没有测试过这个,但是因为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];

您可以使用backItem属性之类的内容来计算图像视图的位置。

如果您想要导航栏右侧的图像,您可以将其定义为自定义按钮,在预设时不执行任何操作,如下所示

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

只需将代码放入- (void)viewWillAppear:(BOOL)animated; 所以它会正常工作并添加一个名为Top Bar的尺寸为320x40的图像

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

导航栏有一个名为标题视图的属性 - 将其设置为您喜欢的图像。 由于titleView会覆盖导航栏的标题,因此您必须在图像文件中包含所需的标题。 仍然将标题设置为您想要的标题,以便在按下视图控制器时它显示在后退按钮上

我遇到了同样的问题。找出最佳解决方案

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

希望这会有所帮助....

只需编写自己的导航栏。 因此,您必须禁用导航栏拳头:

通过在故事板中选择导航控制器来禁用界面构建器中的顶部栏:属性检查器 - >模拟度量标准 - >顶栏:选择无

之后你可以添加你喜欢的任何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