我可以将图像设置为UINavigationBar的标题吗?

Ami*_*ela 66 cocoa-touch ios

是否可以将某些图像设置为导航栏的标题?

我认为NYTimes应用程序使用导航栏,标题看起来像图像文件(之所以看起来UINavigationBar是因为他们使用右键搜索).

Ken*_*ner 128

您可以使用UIImageViewUINavigationItem.titleView财产,是这样的:

self.navigationItem.titleView = myImageView;
Run Code Online (Sandbox Code Playgroud)

  • 不一定与问题相关,但是如果你想在默认标题和图像标题之间交替,你可以通过将`self.navigationItem.titleView`设置为`nil'来回到默认标题. (12认同)
  • 这在iOS6中不再有效.图像会拉伸和畸形. (2认同)
  • 在IOS6中,您现在可以为导航栏设置背景图像.但是,如果您使用此代码并看到图像已拉伸,则表示您没有在放入titleView的UIImageView上正确设置contentMode. (2认同)
  • 对于iOS6添加[imageView setContentMode:UIViewContentModeScaleAspectFit]; (2认同)

Cli*_*urt 16

我发现高度约为35px的透明.png效果很好.

- (void)awakeFromNib {

//put logo image in the navigationBar

UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
self.navigationItem.titleView = img;
[img release];

}
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:我认为该行需要发布. (2认同)

小智 12

您可以从故事板(从Xcode 7开始)直接执行此操作:

  1. 在视图控制器的主视图外创建视图.它可以是嵌套视图,也可以只是图像
  2. 将导航项添加到视图控制器
  3. 按住Ctrl键并从导航项拖动并放在外部视图上

在此输入图像描述

4.选择标题视图

在此输入图像描述


ada*_*dam 8

我已经为UINavigationBar创建了一个自定义类别,如下所示

UINavigationBar的+ CustomImage.h

#import <UIKit/UIKit.h>

@interface UINavigationBar (CustomImage)
    - (void) setBackgroundImage:(UIImage*)image;
    - (void) clearBackgroundImage;
    - (void) removeIfImage:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)

UINavigationBar的+ CustomImage.m

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)

- (void) setBackgroundImage:(UIImage*)image {
    if (image == NULL) return;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(110,5,100,30);
    [self addSubview:imageView];
    [imageView release];
}

- (void) clearBackgroundImage {
    NSArray *subviews = [self subviews];
    for (int i=0; i<[subviews count]; i++) {
        if ([[subviews objectAtIndex:i]  isMemberOfClass:[UIImageView class]]) {
        [[subviews objectAtIndex:i] removeFromSuperview];
    }
   }    
}

@end    
Run Code Online (Sandbox Code Playgroud)

我从我的UINavigationController调用它

[[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image];
Run Code Online (Sandbox Code Playgroud)


小智 7

这条线对你有用,我总是用它

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"]  forBarMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)


ck.*_*ck. 5

我修改了UINavigationBar + CustomImage.m以使标题对用户仍然可见.只需使用insertSubview:atIndex:而不是addSubview:

UINavigationBar的+ CustomImage.m

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)

- (void) setBackgroundImage:(UIImage*)image {
    if (image == NULL) return;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0, 0, 320, 44);
    [self insertSubview:imageView atIndex:0];
    [imageView release];
}

- (void) clearBackgroundImage {
    NSArray *subviews = [self subviews];
    for (int i=0; i<[subviews count]; i++) {
        if ([[subviews objectAtIndex:i]  isMemberOfClass:[UIImageView class]]) {
        [[subviews objectAtIndex:i] removeFromSuperview];
    }
   }    
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 你好.标题仅在即时视图中保持可见,如果用户导航到其他视图(使用navigationController pushViewController),则仅显示背景.我该如何解决这个问题? (2认同)

MrC*_*ker 5

这也很有效

[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
Run Code Online (Sandbox Code Playgroud)