react-native-navigation的迁移库

RNNTopTabsViewController.m 3.2KB

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