简体   繁体   English

如何在导航栏控制器中的标题旁边放置图像?

[英]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.

相关问题 如何更改图像选择器页面的导航栏标题? - How do I change title for navigation bar for image picker page? 如何在导航控制器工具栏的按钮栏上添加标题和图像 - How can I add title and image on a bar buttonitem for a toolbar of navigation controller 如何使用Swift将文本字段的文本作为下一个导航控制器的标题? - How do I make a text field's text the title of the next navigation controller using Swift? 我如何在 swift ios 的导航栏标题中使用图像 - How i can use image in navigation bar title in swift ios 如何在UIViewController中添加带有图像而不是标题的导航栏? - How can I add navigation bar with an image instead of a title in a UIViewController? 如何在导航栏中设置backBarButtonItem标题的样式? - How do I style the backBarButtonItem title in a navigation bar? 如何更改导航栏上“后退”按钮的标题 - How do I change the title of the "back" button on a Navigation Bar 如何在 SwiftUI 中更改导航栏标题的文本属性? - How do I change text attributes for a navigation bar title in SwiftUI? 如何在大型导航栏上将图像用作标题? - How to Use Image as title on large Navigation Bar? iOS:如何在导航栏中与 NavigationBar Title 平行放置一个按钮? - iOS: How to place a button in Navigation bar parallel to NavigationBar Title?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM