UINavigationBar 导航栏背景设置

UINavigationBar 导航栏背景设置,第1张

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

核心代码为:

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

@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]];
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存