67

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

我认为 NYTimes 应用程序使用了导航栏,并且标题看起来像图像文件(看起来的原因UINavigationBar是因为他们使用右键搜索)。

4

16 回答 16

131

您可以使用属性,例如UIImageViewUINavigationItem.titleView

self.navigationItem.titleView = myImageView;
于 2008-11-14T07:45:54.157 回答
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];

}
于 2009-02-23T18:38:40.233 回答
13

您可以直接从情节提要中执行此操作(从 Xcode 7 开始):

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

在此处输入图像描述

4.选择标题视图

在此处输入图像描述

于 2016-03-13T06:39:05.353 回答
8

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

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];
于 2008-11-17T15:31:07.603 回答
7

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

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imageNavBar.png"]  forBarMetrics:UIBarMetricsDefault];
于 2012-10-01T19:57:55.123 回答
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
于 2009-01-09T15:15:03.687 回答
5

这也很好用

[self.navigationController.navigationBar.topItem setTitleView:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"YourLogo"]]];
于 2015-06-17T14:19:37.680 回答
3

使用故事板快速完成,并且@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
        }
    }
}

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

在此处输入图像描述

并等待一秒钟的结果:

在此处输入图像描述

所以设置视图就在它应该在的地方......在故事板中。

于 2016-12-15T16:41:47.440 回答
2

对于那些有相同错误但 inXamarin Forms的人,解决方案是创建一个RendereriniOS应用程序并像这样设置图像:

[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;
            }
        }
    }
}

希望它可以帮助某人!

于 2016-06-20T14:29:54.937 回答
0

只需使用

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

其中 myImage 属于 UIImageView 类型,而 navController 属于 UINavigationController 类型

于 2010-02-25T13:05:41.383 回答
0

我修改了 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];
}
于 2010-11-03T08:43:26.880 回答
0

以下是使用 C#.NET 在(Xamarin 的)MonoTouch 中执行此操作的方法

在 NavigationController 中创建一个 UIViewConvrtoller,然后随时调用它:

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

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

此示例允许视图填充完整的 Navigation Bar ,并让项目标题的文本也出现。

于 2011-09-12T02:56:07.297 回答
0

如果您在导航来回导航时按钮消失,这为我修复了它:

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];
    }
}
于 2011-11-16T10:40:11.520 回答
0

ios5.0 引入了一大堆功能来自定义标准元素的外观。如果您不想使用 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]];
}
于 2012-08-22T04:07:32.230 回答
0

在 MonoTouch 中,您可以使用:

 this.NavigationItem.TitleView = myImageView;
于 2012-09-10T21:22:38.820 回答
0

使用 SWIFT将图像添加到 naviagtionBar,该SWIFT可以缩放以适应并剪辑到边界。您可以在视图控制器 viewDidLoad() 函数中调用此函数。

func setupNavigationBarWithTitleImage(titleImage: UIImage) {

    let imageView = UIImageView(image: titleImage)
    imageView.contentMode = .ScaleAspectFit
    imageView.clipsToBounds = true
    navigationItem.titleView = imageView

}
于 2016-08-19T11:10:32.440 回答