react-native-navigation的迁移库

TopBarPresenter.m 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 (self.transparent) {
  58. [self setBackgroundColorTransparent];
  59. } else if (_backgroundColor) {
  60. self.navigationController.navigationBar.translucent = NO;
  61. self.navigationController.navigationBar.barTintColor = _backgroundColor;
  62. } else if (_translucent) {
  63. self.navigationController.navigationBar.translucent = YES;
  64. } else {
  65. self.navigationController.navigationBar.translucent = NO;
  66. self.navigationController.navigationBar.barTintColor = nil;
  67. }
  68. }
  69. - (void)setBackgroundColorTransparent {
  70. self.navigationController.navigationBar.barTintColor = UIColor.clearColor;
  71. self.navigationController.navigationBar.translucent = YES;
  72. self.navigationController.navigationBar.shadowImage = [UIImage new];
  73. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  74. }
  75. - (void)setTitleAttributes:(RNNTitleOptions *)titleOptions {
  76. NSString* fontFamily = [titleOptions.fontFamily getWithDefaultValue:nil];
  77. NSString* fontWeight = [titleOptions.fontWeight getWithDefaultValue:nil];
  78. NSNumber* fontSize = [titleOptions.fontSize getWithDefaultValue:nil];
  79. UIColor* fontColor = [titleOptions.color getWithDefaultValue:nil];
  80. self.navigationController.navigationBar.titleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.titleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  81. }
  82. - (void)setLargeTitleAttributes:(RNNLargeTitleOptions *)largeTitleOptions {
  83. NSString* fontFamily = [largeTitleOptions.fontFamily getWithDefaultValue:nil];
  84. NSString* fontWeight = [largeTitleOptions.fontWeight getWithDefaultValue:nil];
  85. NSNumber* fontSize = [largeTitleOptions.fontSize getWithDefaultValue:nil];
  86. UIColor* fontColor = [largeTitleOptions.color getWithDefaultValue:nil];
  87. if (@available(iOS 11.0, *)) {
  88. self.navigationController.navigationBar.largeTitleTextAttributes = [RNNFontAttributesCreator createFromDictionary:self.navigationController.navigationBar.largeTitleTextAttributes fontFamily:fontFamily fontSize:fontSize defaultFontSize:nil fontWeight:fontWeight color:fontColor defaultColor:nil];
  89. }
  90. }
  91. - (void)setBackButtonIcon:(UIImage *)icon withColor:(UIColor *)color title:(NSString *)title showTitle:(BOOL)showTitle {
  92. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  93. NSArray* stackChildren = self.navigationController.viewControllers;
  94. icon = color
  95. ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  96. : icon;
  97. [self setBackIndicatorImage:icon withColor:color];
  98. UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
  99. if (showTitle) {
  100. backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
  101. }
  102. backItem.tintColor = color;
  103. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  104. }
  105. - (BOOL)transparent {
  106. return (self.backgroundColor && CGColorGetAlpha(self.backgroundColor.CGColor) == 0.0);
  107. }
  108. @end