【问题标题】:Remove Background Image on UINavigationBar删除 UINavigationBar 上的背景图像
【发布时间】:2012-01-13 03:00:20
【问题描述】:

我的应用程序使用导航控制器,并在主页的导航栏中显示图像。背景图片是通过放置来完成的

[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];

在应用委托中。导航栏很好地显示了这个图像。

所有其他视图不会在导航栏中显示图像。因此,当推送任何其他视图控制器时,我需要删除此背景图像。有谁知道如何做到这一点?

网上有很多关于添加或更改导航栏图像的答案,但这个问题有点不同。先感谢您。

【问题讨论】:

    标签: ios uinavigationcontroller uiimage uinavigationbar


    【解决方案1】:

    要在 iOS 5 或更高版本中删除 UINavigationBar 背景,您应该这样做:

    [navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    

    希望对你有帮助

    【讨论】:

    • 这看起来应该可以工作,但是如果我传递一个真实的图像,这个功能只会改变我的任何东西。
    • 不起作用。正如 rgbrgb 所说。它只对有效图像执行某些操作。
    • XCode 6.4 将不接受图像的 nil 值。
    【解决方案2】:

    我相信这是 iOS5 的第一个实际答案,主要问题是完成后“删除”背景图像。好吧,只需保留现有图像,完成后将其放回即可。

    @implementation MyViewController {
        UIImage *_defaultImage;
    }
    
    - (void)viewWillAppear:(BOOL)animated {   
        _defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
    }
    

    【讨论】:

    • 有趣的方法!
    【解决方案3】:

    您可以使用在运行时创建的空白图像来设置背景图像。

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
    UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
    

    在运行时创建空白图像: How to create a blank transparent png in Objective-C?

    【讨论】:

      【解决方案4】:

      这里没有什么对我有用。在搜索了几个小时后,唯一有效的方法是关注 Apple 提供的 source code example

      我放置了下面的代码,它工作并最终从其他视图控制器中删除了导航栏图像!!

      // Reset everything about navigation bar. (Other flows' background image used to show up on another view controller)
          self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
          self.navigationController!.navigationBar.setBackgroundImage(nil, for: .compact)
          self.navigationController!.navigationBar.barTintColor = nil
          self.navigationController!.toolbar.barTintColor = nil
          self.navigationController!.toolbar.isTranslucent = true
      

      希望这对某人有所帮助。

      干杯

      【讨论】:

        【解决方案5】:

        看起来最简单的方法是:

        1) 设置首页导航栏的alpha为0.000001

        2) 将背景图像放在导航栏“后面”的 UIImageView 中(现在很清楚)

        3) 当视图被推送/弹出时,使用 viewWillAppear / viewWillDisappear 将导航栏的不透明度设置为 1 或 0.00001。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多