react-native-navigation的迁移库

TopBarTitlePresenter.m 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #import "TopBarTitlePresenter.h"
  2. #import "UIViewController+RNNOptions.h"
  3. #import "RNNTitleViewHelper.h"
  4. #import "RNNReactTitleView.h"
  5. @implementation TopBarTitlePresenter {
  6. RNNReactTitleView* _customTitleView;
  7. RNNTitleViewHelper* _titleViewHelper;
  8. }
  9. - (void)applyOptions:(RNNTopBarOptions *)options {
  10. [self updateTitleWithOptions:options];
  11. }
  12. - (void)updateTitleWithOptions:(RNNTopBarOptions *)options {
  13. if (options.title.component.hasValue) {
  14. [self setCustomNavigationTitleView:options perform:nil];
  15. }else if (options.subtitle.text.hasValue) {
  16. [self setTitleViewWithSubtitle:options];
  17. }else if (options.title.text.hasValue) {
  18. [self removeTitleComponents];
  19. self.boundViewController.navigationItem.title = options.title.text.get;
  20. }
  21. }
  22. - (void)mergeOptions:(RNNTopBarOptions *)options resolvedOptions:(RNNTopBarOptions *)resolvedOptions {
  23. if (options.title.component.hasValue) {
  24. [self setCustomNavigationTitleView:resolvedOptions perform:nil];
  25. } else if (options.subtitle.text.hasValue) {
  26. [self setTitleViewWithSubtitle:resolvedOptions];
  27. } else if (options.title.text.hasValue) {
  28. [self removeTitleComponents];
  29. self.boundViewController.navigationItem.title = resolvedOptions.title.text.get;
  30. }
  31. }
  32. - (void)setTitleViewWithSubtitle:(RNNTopBarOptions *)options {
  33. if (!_customTitleView && ![options.largeTitle.visible getWithDefaultValue:NO]) {
  34. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.title subTitleOptions:options.subtitle viewController:self.boundViewController];
  35. if (options.title.text.hasValue) {
  36. [_titleViewHelper setTitleOptions:options.title];
  37. }
  38. if (options.subtitle.text.hasValue) {
  39. [_titleViewHelper setSubtitleOptions:options.subtitle];
  40. }
  41. [_titleViewHelper setup];
  42. }
  43. }
  44. - (void)renderComponents:(RNNTopBarOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  45. [self setCustomNavigationTitleView:options perform:readyBlock];
  46. }
  47. - (void)setCustomNavigationTitleView:(RNNTopBarOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  48. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  49. if (![options.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  50. readyBlock();
  51. readyBlock = nil;
  52. }
  53. if (options.title.component.name.hasValue) {
  54. _customTitleView = (RNNReactTitleView *)[self.componentRegistry createComponentIfNotExists:options.title.component parentComponentId:viewController.layoutInfo.componentId componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:readyBlock];
  55. _customTitleView.backgroundColor = UIColor.clearColor;
  56. NSString* alignment = [options.title.component.alignment getWithDefaultValue:@""];
  57. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  58. [_customTitleView layoutIfNeeded];
  59. viewController.navigationItem.titleView = nil;
  60. viewController.navigationItem.titleView = _customTitleView;
  61. [_customTitleView componentDidAppear];
  62. } else {
  63. [_customTitleView removeFromSuperview];
  64. if (readyBlock) {
  65. readyBlock();
  66. }
  67. }
  68. }
  69. - (void)removeTitleComponents {
  70. [_customTitleView componentDidDisappear];
  71. [_customTitleView removeFromSuperview];
  72. _customTitleView = nil;
  73. self.boundViewController.navigationItem.titleView = nil;
  74. }
  75. - (void)componentDidAppear {
  76. [_customTitleView componentDidAppear];
  77. }
  78. - (void)componentDidDisappear {
  79. [_customTitleView componentDidDisappear];
  80. }
  81. @end