react-native-navigation的迁移库

RNNTopTabsViewController.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #import "RNNTopTabsViewController.h"
  2. #import "RNNSegmentedControl.h"
  3. #import "ReactNativeNavigation.h"
  4. #import "RNNRootViewController.h"
  5. @interface RNNTopTabsViewController () {
  6. NSArray* _viewControllers;
  7. UIViewController<RNNParentProtocol>* _currentViewController;
  8. RNNSegmentedControl* _segmentedControl;
  9. }
  10. @end
  11. @implementation RNNTopTabsViewController
  12. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo childViewControllers:(NSArray *)childViewControllers options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions presenter:(RNNViewControllerPresenter *)presenter {
  13. self = [self init];
  14. self.presenter = presenter;
  15. [self.presenter bindViewController:self];
  16. self.defaultOptions = defaultOptions;
  17. self.options = options;
  18. self.layoutInfo = layoutInfo;
  19. [self setViewControllers:childViewControllers];
  20. return self;
  21. }
  22. - (instancetype)init {
  23. self = [super init];
  24. [self.view setBackgroundColor:[UIColor whiteColor]];
  25. self.edgesForExtendedLayout = UIRectEdgeNone;
  26. [self createTabBar];
  27. [self createContentView];
  28. return self;
  29. }
  30. - (void)onChildWillAppear {
  31. [_presenter applyOptions:self.resolveOptions];
  32. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  33. }
  34. - (RNNNavigationOptions *)resolveOptions {
  35. return [(RNNNavigationOptions *)[self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  36. }
  37. - (void)mergeOptions:(RNNNavigationOptions *)options {
  38. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  39. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  40. }
  41. - (void)overrideOptions:(RNNNavigationOptions *)options {
  42. [self.options overrideOptions:options];
  43. }
  44. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  45. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  46. dispatch_group_t group = dispatch_group_create();
  47. for (UIViewController<RNNLayoutProtocol>* childViewController in self.childViewControllers) {
  48. dispatch_group_enter(group);
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. [childViewController renderTreeAndWait:wait perform:^{
  51. dispatch_group_leave(group);
  52. }];
  53. });
  54. }
  55. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  56. dispatch_async(dispatch_get_main_queue(), ^{
  57. readyBlock();
  58. });
  59. });
  60. }
  61. - (void)createTabBar {
  62. _segmentedControl = [[RNNSegmentedControl alloc] initWithSectionTitles:@[@"", @"", @""]];
  63. _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
  64. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
  65. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
  66. _segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
  67. [_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  68. [self.view addSubview:_segmentedControl];
  69. }
  70. - (void)segmentedControlChangedValue:(HMSegmentedControl*)segmentedControl {
  71. [self setSelectedViewControllerIndex:segmentedControl.selectedSegmentIndex];
  72. }
  73. - (void)createContentView {
  74. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
  75. _contentView.backgroundColor = [UIColor grayColor];
  76. [self.view addSubview:_contentView];
  77. }
  78. - (void)setSelectedViewControllerIndex:(NSUInteger)index {
  79. UIViewController<RNNParentProtocol> *toVC = _viewControllers[index];
  80. [_contentView addSubview:toVC.view];
  81. [_currentViewController.view removeFromSuperview];
  82. _currentViewController = toVC;
  83. }
  84. - (void)setViewControllers:(NSArray *)viewControllers {
  85. _viewControllers = viewControllers;
  86. for (RNNRootViewController* childVc in viewControllers) {
  87. [childVc.view setFrame:_contentView.bounds];
  88. // [childVc.options.topTab applyOn:childVc];
  89. [self addChildViewController:childVc];
  90. }
  91. [self setSelectedViewControllerIndex:0];
  92. }
  93. - (void)viewController:(UIViewController*)vc changedTitle:(NSString*)title {
  94. NSUInteger vcIndex = [_viewControllers indexOfObject:vc];
  95. [_segmentedControl setTitle:title atIndex:vcIndex];
  96. }
  97. - (void)viewDidLoad {
  98. [super viewDidLoad];
  99. }
  100. #pragma mark RNNParentProtocol
  101. - (UIViewController *)getCurrentChild {
  102. return _currentViewController;
  103. }
  104. @end