react-native-navigation的迁移库

RNNTopTabsViewController.m 2.9KB

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