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

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

我觉得NYTimes应用程序使用了一个导航栏和标题看起来像图像文件(它似乎UINavigationBar的原因是因为他们使用右键search)。

你可以使用一个UIImageView作为UINavigationItem.titleView属性,如下所示:

 self.navigationItem.titleView = myImageView; 

我发现一个大约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]; } 

你可以直接从故事板(从Xcode 7开始):

  1. 在视图控制器的主视图之外创build一个视图。 它可以是嵌套视图或只是一个图像
  2. 将导航项目添加到您的视图控制器
  3. 按住Ctrl键并从导航项中拖动并放到外部视图中

在这里输入图像说明

4.select标题视图

在这里输入图像说明

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

UINavigationBar的+ CustomImage.h

 #import <UIKit/UIKit.h> @interface UINavigationBar (CustomImage) - (void) setBackgroundImage:(UIImage*)image; - (void) clearBackgroundImage; - (void) removeIfImage:(id)sender; @end 

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 

我从我的UINavigationController调用它

 [[navController navigationBar] performSelectorInBackground:@selector(setBackgroundImage:) withObject:image]; 

这条线将为你工作,我总是使用这个

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

我修改了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 

这也可以

 [self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]]; 

对于那些有相同的错误,但在Xamarin Forms ,解决scheme是在iOS应用程序中创build一个Renderer ,并像这样设置图像:

 [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyApp.Renderers.NavigationPageRenderer))] namespace MyApp.Renderers { #region using using UIKit; using Xamarin.Forms.Platform.iOS; #endregion public class NavigationPageRenderer : PageRenderer { public override void ViewDidLoad() { base.ViewDidLoad(); SetTitleImage(); } private void SetTitleImage() { UIImage logoImage = UIImage.FromFile(ResourceFiles.ImageResources.LogoImageName); UIImageView logoImageView = new UIImageView(logoImage); if (this.NavigationController != null) { this.NavigationController.NavigationBar.TopItem.TitleView = logoImageView; } } } } 

希望它可以帮助别人!

快速使用storyboard和@IBDesignable

 @IBDesignable class AttributedNavigationBar: UINavigationBar { @IBInspectable var imageTitle: UIImage? = nil { didSet { guard let imageTitle = imageTitle else { topItem?.titleView = nil return } let imageView = UIImageView(image: imageTitle) imageView.frame = CGRect(x: 0, y: 0, width: 40, height: 30) imageView.contentMode = .scaleAspectFit topItem?.titleView = imageView } } } 

然后在属性检查器中select一个图像:

在这里输入图像说明

等待第二个结果:

在这里输入图像说明

所以设置视图应该在故事板中。

我修改了UINavigationBar + CustomImage代码以正常工作而不会泄漏内存。

 - (void)setBackgroundImage:(UIImage *)image { if (! image) return; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); [self addSubview:imageView]; [imageView release]; } - (void) clearBackgroundImage { // This runs on a separate thread, so give it it's own pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *mySubviews = self.subviews; // Move in reverse direction as not to upset the order of elements in the array for (int i = [mySubviews count] - 1; i >= 0; i--) { if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) { [[mySubviews objectAtIndex:i] removeFromSuperview]; } } [pool release]; } 

以下是您将如何使用C#.NET在(Xamarin's)MonoTouch中执行此操作

创build一个位于NavigationController中的UIViewConvrtoller,然后随时调用它:

 someNiceViewControllerYouMade.NavigationController.NavigationBar .InsertSubview(new UIImageView (MediaProvider.GetImage(ImageGeneral.navBar_667x44)),0); 

注意:MediaProvider只是一个提取图像的类。

此示例允许视图填充完整的导航栏,并让项目标题的文本也显示出来。

如果你的button消失,当你导航来回导航,这固​​定它给我:

 NSArray *mySubviews = navigationBar.subviews; UIImageView *iv = nil; // Move in reverse direction as not to upset the order of elements in the array for (int i = [mySubviews count] - 1; i >= 0; i--) { if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]]) { NSLog(@"found background at index %d",i); iv = [mySubviews objectAtIndex:i]; [[mySubviews objectAtIndex:i] removeFromSuperview]; [navigationBar insertSubview:iv atIndex:0]; } } 

只是使用

 [navController.navigationBar insertSubview:myImage atIndex:0] ; 

其中myImage的types是UIImageView,navController的types是UINavigationController

ios5.0引入了一堆function来自定义标准元素的外观。 如果您不想使用ImageView作为标题,则可以使用背景图像和自定义字体/颜色自定义所有UINavbars的外观。

 - (void) customiseMyNav { // Create resizable images UIImage *portraitImage = [[UIImage imageNamed:@"nav_bar_bg_portrait"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; UIImage *landscapeImage = [[UIImage imageNamed:@"nav_bar_bg_landscape"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; // Set the background image [[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone]; // set the title appearance [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:50.0/255.0 green:150.0/255.0 blue:100/255.0 alpha:1.0], UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Arial-Bold" size:0.0], UITextAttributeFont, nil]]; } 

在MonoTouch中,你可以使用这个:

  this.NavigationItem.TitleView = myImageView; 

使用SWIFT将图像添加到naviagtionBar,然后将其缩放以适合边界。 你可以在视图控制器的viewDidLoad()函数里面调用这个函数。

 func setupNavigationBarWithTitleImage(titleImage: UIImage) { let imageView = UIImageView(image: titleImage) imageView.contentMode = .ScaleAspectFit imageView.clipsToBounds = true navigationItem.titleView = imageView }