react-native-navigation的迁移库

RNNTopTabsViewController.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.getCurrentChild.resolveOptions.copy mergeOptions:self.options] 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)createTabBar {
  45. _segmentedControl = [[RNNSegmentedControl alloc] initWithSectionTitles:@[@"", @"", @""]];
  46. _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
  47. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
  48. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
  49. _segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
  50. [_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  51. [self.view addSubview:_segmentedControl];
  52. }
  53. - (void)segmentedControlChangedValue:(HMSegmentedControl*)segmentedControl {
  54. [self setSelectedViewControllerIndex:segmentedControl.selectedSegmentIndex];
  55. }
  56. - (void)createContentView {
  57. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
  58. _contentView.backgroundColor = [UIColor grayColor];
  59. [self.view addSubview:_contentView];
  60. }
  61. - (void)setSelectedViewControllerIndex:(NSUInteger)index {
  62. UIViewController<RNNParentProtocol> *toVC = _viewControllers[index];
  63. [_contentView addSubview:toVC.view];
  64. [_currentViewController.view removeFromSuperview];
  65. _currentViewController = toVC;
  66. }
  67. - (void)setViewControllers:(NSArray *)viewControllers {
  68. _viewControllers = viewControllers;
  69. for (RNNRootViewController* childVc in viewControllers) {
  70. [childVc.view setFrame:_contentView.bounds];
  71. // [childVc.options.topTab applyOn:childVc];
  72. }
  73. [self setSelectedViewControllerIndex:0];
  74. }
  75. - (void)viewController:(UIViewController*)vc changedTitle:(NSString*)title {
  76. NSUInteger vcIndex = [_viewControllers indexOfObject:vc];
  77. [_segmentedControl setTitle:title atIndex:vcIndex];
  78. }
  79. - (void)viewDidLoad {
  80. [super viewDidLoad];
  81. }
  82. #pragma mark RNNParentProtocol
  83. - (UIViewController *)getCurrentChild {
  84. return _currentViewController;
  85. }
  86. - (UIViewController<RNNLeafProtocol> *)getCurrentLeaf {
  87. return [[self getCurrentChild] getCurrentLeaf];
  88. }
  89. @end