react-native-navigation的迁移库

TopBarPresenter.m 6.1KB

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