react-native-navigation的迁移库

RNNTopTabsViewController.m 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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:(RNNViewControllerPresenter *)presenter {
  13. self = [self init];
  14. self.presenter = presenter;
  15. [self.presenter bindViewController:self];
  16. self.options = options;
  17. self.layoutInfo = layoutInfo;
  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)onChildWillAppear {
  30. [_presenter applyOptions:self.resolveOptions];
  31. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  32. }
  33. - (RNNNavigationOptions *)resolveOptions {
  34. return (RNNNavigationOptions *)[self.getCurrentChild.resolveOptions.copy mergeOptions:self.options];
  35. }
  36. - (void)mergeOptions:(RNNNavigationOptions *)options {
  37. [_presenter mergeOptions:options];
  38. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  39. }
  40. - (void)createTabBar {
  41. _segmentedControl = [[RNNSegmentedControl alloc] initWithSectionTitles:@[@"", @"", @""]];
  42. _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
  43. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
  44. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
  45. _segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
  46. [_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  47. [self.view addSubview:_segmentedControl];
  48. }
  49. - (void)segmentedControlChangedValue:(HMSegmentedControl*)segmentedControl {
  50. [self setSelectedViewControllerIndex:segmentedControl.selectedSegmentIndex];
  51. }
  52. - (void)createContentView {
  53. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
  54. _contentView.backgroundColor = [UIColor grayColor];
  55. [self.view addSubview:_contentView];
  56. }
  57. - (void)setSelectedViewControllerIndex:(NSUInteger)index {
  58. UIViewController<RNNParentProtocol> *toVC = _viewControllers[index];
  59. [_contentView addSubview:toVC.view];
  60. [_currentViewController.view removeFromSuperview];
  61. _currentViewController = toVC;
  62. }
  63. - (void)setViewControllers:(NSArray *)viewControllers {
  64. _viewControllers = viewControllers;
  65. for (RNNRootViewController* childVc in viewControllers) {
  66. [childVc.view setFrame:_contentView.bounds];
  67. // [childVc.options.topTab applyOn:childVc];
  68. }
  69. [self setSelectedViewControllerIndex:0];
  70. }
  71. - (void)viewController:(UIViewController*)vc changedTitle:(NSString*)title {
  72. NSUInteger vcIndex = [_viewControllers indexOfObject:vc];
  73. [_segmentedControl setTitle:title atIndex:vcIndex];
  74. }
  75. - (void)viewDidLoad {
  76. [super viewDidLoad];
  77. }
  78. #pragma mark RNNParentProtocol
  79. - (UIViewController *)getCurrentChild {
  80. return _currentViewController;
  81. }
  82. @end