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

eba*_*unt 22 iphone

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

小智 36

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

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

self.navigationItem.titleView = imageView;

[imageView release];
Run Code Online (Sandbox Code Playgroud)

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


Rog*_*Rog 9

我没有测试过这个,但是因为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];
Run Code Online (Sandbox Code Playgroud)

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


Don*_*air 6

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

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


zpe*_*esk 2

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

  • 您不必替换标题视图,您可以创建一个 UIImageView,加载图标图像,并将其添加为标题视图的子视图。使用 UIImageView 的框架属性将其放置在您想要的位置。 (2认同)