我在xib中添加了导航栏和导航项。现在我需要在导航栏的左侧添加图像,但它没有被添加。 这是我正在处理的代码:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
我无法理解为什么没有添加图像。我该如何添加它?
答案 0 :(得分:7)
看起来这是一个老问题,并且已接受答案。但是,我想补充一下:
至少在Xcode 5中,如果你正在使用故事板那么你可以通过将空视图拖到标题区域,然后将ImageView放在空视图中来将图像添加为navigationBar的标题
以下屏幕截图可让您了解它的外观:
答案 1 :(得分:6)
@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
<强>更新强>
只需在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;
如果你想在NavigationBar的背景中直接设置图像,那么使用下面的线...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
另见此链接how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk
我希望这可以帮助你...
答案 2 :(得分:3)
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:@"Your Image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 10, 72, 19);
[self.navViewController.navigationBar addSubview:imageView];
}
答案 3 :(得分:1)
如果要在导航栏中的特定点设置图像,则可以创建视图,然后在视图中添加图像,调整图像框,然后将此视图添加到导航栏。
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];
如果您要从xib添加NavBar,那么只需让IBOutlet
为UINavigationBar
连接xib然后
[myNavigationBar addSubview:view];
答案 4 :(得分:1)
For Swift 4:
override func viewDidAppear(_ animated: Bool) {
// 1
let nav = self.navigationController?.navigationBar
// 2
nav?.barStyle = UIBarStyle.black
nav?.tintColor = UIColor.yellow
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
// 4
let image = UIImage(named: "Apple_Swift_Logo")
imageView.image = image
// 5
navigationItem.titleView = imageView
}
答案 5 :(得分:0)
尝试设置 imageView.frame 也请正确交叉检查图片已启动
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];
答案 6 :(得分:0)
您的代码看起来不错。但我不知道你为什么需要在xib中添加导航栏和项目。如果您创建自己的视图控制器派生自UIViewController,它包含导航栏。您可以尝试从xib中删除自己添加的导航栏和项目,看看发生了什么。
如果要在基于视图的应用程序的第一个视图上显示导航栏,则需要修改应用程序类代码,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
// REMOVE THIS LINE !!!!!!!!!!!!!
//self.window.rootViewController = self.viewController;
// ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
我刚做了如下测试:
在viewcontroller.m中,添加以下行。
- (无效)viewDidLoad中 { [super viewDidLoad]; UIImageView * view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)]; view.image = [UIImage imageNamed:@“info.png”]; self.navigationItem.titleView = view; }
现在再次运行。您将看到一个带有图像标题的导航栏。
答案 7 :(得分:0)
我希望这会对你有所帮助。如果不起作用,请检查您的图像名称是否正确。
UIImage *yourimage = [UIImage imageNamed: @"i_launcher.png"];
UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;
如果您希望添加操作意味着:
[buttonBarButton addTarget:self action:@selector(your function) forControlEvents:UIControlEventTouchUpInside];
答案 8 :(得分:0)
隐藏/显示移动到另一个控制器之间的图像执行此操作。相应地调整CGRectMake尺寸。
将此添加到您的标题文件中:
@property (weak, nonatomic) UIImageView *navBarLogo;
将其添加到.m控制器文件中:
- (void)viewWillAppear:(BOOL)animated {
UIImage *image = [UIImage imageNamed:@"image_name.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(53, 7, 210, 30);
self.navBarLogo = imageView;
[self.navigationController.navigationBar addSubview:imageView];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}