react-native-navigation的迁移库

RNNTopTabsViewController.m 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #import "RNNTopTabsViewController.h"
  2. #import "RNNSegmentedControl.h"
  3. #import "RNNRootViewController.h"
  4. #import "ReactNativeNavigation.h"
  5. @interface RNNTopTabsViewController () {
  6. NSArray* _viewControllers;
  7. UIViewController<RNNRootViewProtocol>* _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<RNNRootViewProtocol> *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 applyTopTabsOptions];
  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. - (BOOL)isCustomTransitioned {
  59. return NO;
  60. }
  61. - (RNNOptions *)options {
  62. return nil;
  63. }
  64. - (NSString *)componentId {
  65. return _currentViewController.componentId;
  66. }
  67. @end