当前位置:   article > 正文

UINavigationBar 导航栏背景设置_self.navigationbar.setbackgroundimage无效

self.navigationbar.setbackgroundimage无效

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

核心代码为:

  1. if (@available(iOS 13.0, *))
  2. {
  3. UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
  4. appperance.backgroundImage = [UIImage imageWithColor:color];
  5. self.navigationBar.standardAppearance = appperance;
  6. if (@available(iOS 15.0, *))
  7. {
  8. self.navigationBar.scrollEdgeAppearance = appperance;
  9. }
  10. #if __has_feature(objc_arc)
  11. #else
  12. [appperance release];
  13. #endif

  1. @interface UINavigationController (addititonal)
  2. - (void)setBackGroundImageView:(NSString*)imageName;
  3. - (void)setBackGroundImageViewWithImage:(UIImage*)image;
  4. - (void)setBackgroundColor:(UIColor*)color;
  5. - (NSArray<__kindof UIViewController *> *)popToViewControllerClass:(NSString *)classStr animated:(BOOL)animated;
  6. - (UIViewController*)rootViewController;
  7. @end
  1. @implementation UINavigationController (addititonal)
  2. - (void)setBackGroundImageView:(NSString*)imageName
  3. {
  4. CGFloat navbarHt = 64+([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom>0?24:0);
  5. UIImage *image = [UIImage imageNamed:imageName];
  6. image = [UIImage image:image fitInSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, navbarHt)];
  7. if (@available(iOS 13.0, *))
  8. {
  9. UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
  10. appperance.backgroundImage = image;
  11. self.navigationBar.standardAppearance = appperance;
  12. if (@available(iOS 15.0, *))
  13. {
  14. self.navigationBar.scrollEdgeAppearance = appperance;
  15. }
  16. #if __has_feature(objc_arc)
  17. #else
  18. [appperance release];
  19. #endif
  20. }
  21. else
  22. {
  23. [self.navigationBar setBackgroundImage:image forBarPosition:UIBarPositionTop barMetrics:UIBarMetricsDefault];
  24. }
  25. }
  26. - (void)setBackGroundImageViewWithImage:(UIImage*)image
  27. {
  28. if (@available(iOS 13.0, *))
  29. {
  30. UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
  31. appperance.backgroundImage = image;
  32. self.navigationBar.standardAppearance = appperance;
  33. if (@available(iOS 15.0, *))
  34. {
  35. self.navigationBar.scrollEdgeAppearance = appperance;
  36. }
  37. #if __has_feature(objc_arc)
  38. #else
  39. [appperance release];
  40. #endif
  41. }
  42. else
  43. {
  44. [self.navigationBar setBackgroundImage:image];
  45. }
  46. }
  47. - (void)setBackgroundColor:(UIColor*)color
  48. {
  49. if (@available(iOS 13.0, *))
  50. {
  51. UINavigationBarAppearance *appperance = [[UINavigationBarAppearance alloc]init];
  52. appperance.backgroundImage = [UIImage imageWithColor:color];
  53. self.navigationBar.standardAppearance = appperance;
  54. if (@available(iOS 15.0, *))
  55. {
  56. self.navigationBar.scrollEdgeAppearance = appperance;
  57. }
  58. #if __has_feature(objc_arc)
  59. #else
  60. [appperance release];
  61. #endif
  62. }
  63. else
  64. {
  65. [self.navigationBar setBackgroundImage:[UIImage imageWithColor:color]];
  66. }
  67. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/219341
推荐阅读
相关标签
  

闽ICP备14008679号