react-native-navigation的迁移库

TopBarPresenter.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #import "TopBarPresenter.h"
  2. #import "UIImage+tint.h"
  3. #import "RNNFontAttributesCreator.h"
  4. @implementation TopBarPresenter
  5. - (instancetype)initWithNavigationController:(UINavigationController *)boundNavigationController {
  6. self = [super init];
  7. self.boundViewController = boundNavigationController;
  8. return self;
  9. }
  10. - (void)applyOptions:(RNNTopBarOptions *)options {
  11. [self setTranslucent:[options.background.translucent getWithDefaultValue:NO]];
  12. [self setBackgroundColor:[options.background.color getWithDefaultValue:nil]];
  13. [self setTitleAttributes:options.title];
  14. [self setLargeTitleAttributes:options.largeTitle];
  15. [self showBorder:![options.noBorder getWithDefaultValue:NO]];
  16. [self setBackButtonIcon:[options.backButton.icon getWithDefaultValue:nil] withColor:[options.backButton.color getWithDefaultValue:nil] title:[options.backButton.title getWithDefaultValue:nil] showTitle:[options.backButton.showTitle getWithDefaultValue:YES]];
  17. }
  18. - (void)applyOptionsBeforePopping:(RNNTopBarOptions *)options {
  19. [self setBackgroundColor:[options.background.color getWithDefaultValue:nil]];
  20. [self setTitleAttributes:options.title];
  21. [self setLargeTitleAttributes:options.largeTitle];
  22. }
  23. - (void)mergeOptions:(RNNTopBarOptions *)options defaultOptions:(RNNTopBarOptions *)defaultOptions {
  24. if (options.background.color.hasValue) {
  25. [self setBackgroundColor:options.background.color.get];
  26. }
  27. if (options.noBorder.hasValue) {
  28. [self showBorder:![options.noBorder get]];
  29. }
  30. if (options.background.translucent.hasValue) {
  31. [self setTranslucent:[options.background.translucent get]];
  32. }
  33. RNNLargeTitleOptions* largeTitleOptions = options.largeTitle;
  34. if (largeTitleOptions.color.hasValue || largeTitleOptions.fontSize.hasValue || largeTitleOptions.fontFamily.hasValue) {
  35. [self setLargeTitleAttributes:largeTitleOptions];
  36. }
  37. [self setTitleAttributes:options.title];
  38. if (options.backButton.hasValue) {
  39. [self setBackButtonIcon:[defaultOptions.backButton.icon getWithDefaultValue:nil] withColor:[defaultOptions.backButton.color getWithDefaultValue:nil] title:[defaultOptions.backButton.title getWithDefaultValue:nil] showTitle:[defaultOptions.backButton.showTitle getWithDefaultValue:YES]];
  40. }
  41. }
  42. - (UINavigationController *)navigationController {
  43. return (UINavigationController *)self.boundViewController;
  44. }
  45. - (void)showBorder:(BOOL)showBorder {
  46. [self.navigationController.navigationBar setShadowImage:showBorder ? nil : [UIImage new]];
  47. }
  48. - (void)setBackIndicatorImage:(UIImage *)image withColor:(UIColor *)color {
  49. [self.navigationController.navigationBar setBackIndicatorImage:image];
  50. [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:image];
  51. }
  52. - (void)setBackgroundColor:(UIColor *)backgroundColor {
  53. _backgroundColor = backgroundColor;
  54. [self updateBackgroundAppearance];
  55. }
  56. - (void)updateBackgroundAppearance {
  57. if (_transparent) {
  58. [self setBackgroundColorTransparent];
  59. } else if (_backgroundColor) {
  60. self.navigationController.navigationBar.barTintColor = _backgroundColor;
  61. } else if (_translucent) {
  62. self.navigationController.navigationBar.translucent = YES;
  63. } else {
  64. self.navigationController.navigationBar.translucent = NO;
  65. self.navigationController.navigationBar.barTintColor = nil;
  66. }
  67. }
  68. - (void)setBackgroundColorTransparent {
  69. self.navigationController.navigationBar.barTintColor = UIColor.clearColor;
  70. self.navigationController.navigationBar.shadowImage = [UIImage new];
  71. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  72. }
  73. - (void)setTitleAttributes:(RNNTitleOptions *)titleOptions {
  74. NSString* fontFamily = [titleOptions.fontFamily getWithDefaultValue:nil];
  75. NSString* fontWeight = [titleOptions.fontWeight getWithDefaultValue:nil];
  76. NSNumber* fontSize = [titleOptions.fontSize getWithDefaultValue:nil];
  77. UIColor* fontColor = [titleOptions.color getWithDefaultValue:nil];
  78. self.navigationController.navigationBar.titleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  79. }
  80. - (void)setLargeTitleAttributes:(RNNLargeTitleOptions *)largeTitleOptions {
  81. NSString* fontFamily = [largeTitleOptions.fontFamily getWithDefaultValue:nil];
  82. NSString* fontWeight = [largeTitleOptions.fontWeight getWithDefaultValue:nil];
  83. NSNumber* fontSize = [largeTitleOptions.fontSize getWithDefaultValue:nil];
  84. UIColor* fontColor = [largeTitleOptions.color getWithDefaultValue:nil];
  85. if (@available(iOS 11.0, *)) {
  86. self.navigationController.navigationBar.largeTitleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  87. }
  88. }
  89. - (void)setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title showTitle:(BOOL)showTitle {
  90. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  91. NSArray* stackChildren = self.navigationController.viewControllers;
  92. icon = color
  93. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  94. : icon;
  95. [self setBackIndicatorImage:icon withColor:color];
  96. UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
  97. if (showTitle) {
  98. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  99. }
  100. backItem.tintColor = color;
  101. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  102. }
  103. @end