之前使用好好的项目,导航栏配置什么的也没有啥问题,最近突然发现导航栏配置无效了,尤其是背景色调整,无论如何也显示不了了。遂进行排查,现将代码整理:
核心代码为:
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]];
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)