【问题标题】:How to setup a navigation bar with image?如何设置带有图像的导航栏?
【发布时间】:2014-03-26 10:53:23
【问题描述】:

UINavigationBariOS7 上发生了一些奇怪和令人沮丧的事情。我正在尝试添加这样的图像:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Logo_Small.png"] forBarMetrics:UIBarMetricsDefault];

结果是我的标题的字母——希伯来字母——在导航栏上重复。

我尝试了UIBarMetrics 标志的所有四个选项,但其他三个标志就消失了。根据文档,我还将图像大小设置为 320x64。但似乎没有什么是正确的,我正处于放弃使用徽标的边缘,只拥有一个 controller.title。

【问题讨论】:

  • 设置图片大小?您的意思是“使用了正确尺寸的图像”?
  • 试试这个,[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithPatternImage:@"Logo_Small.png"]]; for iOS7 with 320x64 image.

标签: ios ios7 uinavigationbar


【解决方案1】:

执行以下操作:

UIImage *gradientImage = [[UIImage imageNamed:@"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-4, 300, 10, 300)];// play here
[[UINavigationBar appearance] setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];

【讨论】:

    【解决方案2】:

    你可以这样做

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
    iv.image = [UIImage imageNamed:@"Logo_Small.png"];
    self.navigationItem.titleView = iv;
    

    【讨论】:

      【解决方案3】:

      使用这个,

         UIImage *navBarImage64 = [[UIImage imageNamed:@"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
         [[UINavigationBar appearance] setBackgroundImage:navBarImage64 forBarMetrics:UIBarMetricsDefault];
      

      【讨论】:

        【解决方案4】:

        如果上述方法不起作用,则在 NavigationBar 中添加 imageview,如下代码所示。它工作。

         UIImage *image = [UIImage imageNamed: @"NavigationBar@2x.png"];    
         UIImageView *imageView = [[UIImageView alloc] initWithImage: image];    
         imageView.frame = CGRectMake(0, 0, 320, 44);   
         [self.navigationController.navigationBar addSubview:imageView];
        

        【讨论】:

          【解决方案5】:

          您的代码确实有效。问题是您可能在 UINavigationBar 添加到屏幕后调用它。

          要验证您的代码是否正常工作,您可以尝试将其放入 AppDelegate 的 didiFinishedLaunchingWithOptions 中。

          【讨论】:

            【解决方案6】:

            要使用 Image 自定义导航栏,

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

            【讨论】:

              猜你喜欢
              • 2016-02-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-03-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多