0
点赞
收藏
分享

微信扫一扫

UINavigationBar 导航栏背景设置

星巢文化 2022-01-05 阅读 96

之前使用好好的项目,导航栏配置什么的也没有啥问题,最近突然发现导航栏配置无效了,尤其是背景色调整,无论如何也显示不了了。遂进行排查,现将代码整理:

核心代码为:

@interface UINavigationController (addititonal)
- (void)setBackGroundImageView:(NSString*)imageName;
- (void)setBackGroundImageViewWithImage:(UIImage*)image;
- (void)setBackgroundColor:(UIColor*)color;

- (NSArray<__kindof UIViewController *> *)popToViewControllerClass:(NSString *)classStr animated:(BOOL)animated;

- (UIViewController*)rootViewController;
@end
@implementation UINavigationController (addititonal)

- (void)setBackGroundImageView:(NSString*)imageName
{
    CGFloat navbarHt = 64+([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom>0?24:0);
    UIImage *image = [UIImage imageNamed:imageName];
    image = [UIImage image:image fitInSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, navbarHt)];
    
    if (@available(iOS 13.0, *))
    {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
        appperance.backgroundImage = image;
        self.navigationBar.standardAppearance = appperance;
        if (@available(iOS 15.0, *))
        {
            self.navigationBar.scrollEdgeAppearance = appperance;
        }
        
#if __has_feature(objc_arc)
#else
        [appperance release];
#endif
    }
    else
    {
        [self.navigationBar setBackgroundImage:image forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
    }
}


- (void)setBackGroundImageViewWithImage:(UIImage*)image
{
    if (@available(iOS 13.0, *))
    {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
        appperance.backgroundImage = image;
        self.navigationBar.standardAppearance = appperance;
        if (@available(iOS 15.0, *))
        {
            self.navigationBar.scrollEdgeAppearance = appperance;
        }
#if __has_feature(objc_arc)
#else
        [appperance release];
#endif
    }
    else
    {
        [self.navigationBar setBackgroundImage:image];
    }
}


- (void)setBackgroundColor:(UIColor*)color
{
    if (@available(iOS 13.0, *))
    {
        UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
        appperance.backgroundImage = [UIImage imageWithColor:color];
        self.navigationBar.standardAppearance = appperance;
        if (@available(iOS 15.0, *))
        {
            self.navigationBar.scrollEdgeAppearance = appperance;
        }
#if __has_feature(objc_arc)
#else
        [appperance release];
#endif
    }
    else
    {
        [self.navigationBar setBackgroundImage:[UIImage imageWithColor:color]];
    }
}
举报

相关推荐

0 条评论