react-native-navigation的迁移库

RNNTopTabsViewController.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 optionsResolver:(RNNParentOptionsResolver *)optionsResolver presenter:(RNNBasePresenter *)presenter {
  13. self = [self init];
  14. self.presenter = presenter;
  15. self.options = options;
  16. self.layoutInfo = layoutInfo;
  17. self.optionsResolver = optionsResolver;
  18. [self setViewControllers:childViewControllers];
  19. return self;
  20. }
  21. - (instancetype)init {
  22. self = [super init];
  23. [self.view setBackgroundColor:[UIColor whiteColor]];
  24. self.edgesForExtendedLayout = UIRectEdgeNone;
  25. [self createTabBar];
  26. [self createContentView];
  27. return self;
  28. }
  29. - (void)createTabBar {
  30. _segmentedControl = [[RNNSegmentedControl alloc] initWithSectionTitles:@[@"", @"", @""]];
  31. _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
  32. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
  33. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
  34. _segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
  35. [_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  36. [self.view addSubview:_segmentedControl];
  37. }
  38. - (void)segmentedControlChangedValue:(HMSegmentedControl*)segmentedControl {
  39. [self setSelectedViewControllerIndex:segmentedControl.selectedSegmentIndex];
  40. }
  41. - (void)createContentView {
  42. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
  43. _contentView.backgroundColor = [UIColor grayColor];
  44. [self.view addSubview:_contentView];
  45. }
  46. - (void)setSelectedViewControllerIndex:(NSUInteger)index {
  47. UIViewController<RNNParentProtocol> *toVC = _viewControllers[index];
  48. [_contentView addSubview:toVC.view];
  49. [_currentViewController.view removeFromSuperview];
  50. _currentViewController = toVC;
  51. }
  52. - (void)setViewControllers:(NSArray *)viewControllers {
  53. _viewControllers = viewControllers;
  54. for (RNNRootViewController* childVc in viewControllers) {
  55. [childVc.view setFrame:_contentView.bounds];
  56. [childVc.options.topTab applyOn:childVc];
  57. }
  58. [self setSelectedViewControllerIndex:0];
  59. }
  60. - (void)viewController:(UIViewController*)vc changedTitle:(NSString*)title {
  61. NSUInteger vcIndex = [_viewControllers indexOfObject:vc];
  62. [_segmentedControl setTitle:title atIndex:vcIndex];
  63. }
  64. - (void)viewDidLoad {
  65. [super viewDidLoad];
  66. }
  67. - (void)willMoveToParentViewController:(UIViewController *)parent {
  68. [_optionsResolver resolve:self with:_viewControllers];
  69. [_presenter present:self.options onViewControllerDidLoad:self];
  70. }
  71. - (void)mergeOptions:(RNNNavigationOptions *)options {
  72. [self.presenter present:options onViewControllerWillAppear:self];
  73. }
  74. #pragma mark RNNParentProtocol
  75. - (UIViewController *)getLeafViewController {
  76. return _currentViewController;
  77. }
  78. @end