首页 文章

删除UINavigationBar上的背景图像

提问于
浏览
5

我的应用程序使用导航控制器,主页导航栏中有图像 . 背景图像是通过推杆完成的

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

在应用程序委托中 . 导航栏显示此图像正常 .

每个其他视图 not 都会在导航栏中显示一个图像 . 因此,我需要在按下任何其他视图控制器时删除此背景图像 . 有谁知道如何做到这一点?

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

5 回答

  • 3

    我相信这是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

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

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

    希望能帮助到你

  • 0

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

    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?

  • 0

    看起来最简单的方法是:

    1)将主页上导航栏的alpha设置为0.000001

    2)将背景图像放在UIImageView“后面”导航栏(现在已清除)

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

  • 5

    这里没有什么对我有用 . 搜索了几个小时之后,唯一有效的方法是跟随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
    

    希望这有助于某人 .

    干杯

相关问题