react-native-navigation的迁移库

RNNTopTabsViewController.m 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #import "RNNTopTabsViewController.h"
  2. #import "RNNSegmentedControl.h"
  3. #import "ReactNativeNavigation.h"
  4. @interface RNNTopTabsViewController () {
  5. NSArray* _viewControllers;
  6. UIViewController<RNNRootViewProtocol>* _currentViewController;
  7. RNNSegmentedControl* _segmentedControl;
  8. }
  9. @end
  10. @implementation RNNTopTabsViewController
  11. - (instancetype)init {
  12. self = [super init];
  13. [self.view setBackgroundColor:[UIColor whiteColor]];
  14. self.edgesForExtendedLayout = UIRectEdgeNone;
  15. [self createTabBar];
  16. [self createContentView];
  17. return self;
  18. }
  19. - (void)createTabBar {
  20. _segmentedControl = [[RNNSegmentedControl alloc] initWithSectionTitles:@[@"", @"", @""]];
  21. _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 50);
  22. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
  23. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleBox;
  24. _segmentedControl.selectedSegmentIndex = HMSegmentedControlNoSegment;
  25. [_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  26. [self.view addSubview:_segmentedControl];
  27. }
  28. - (void)segmentedControlChangedValue:(HMSegmentedControl*)segmentedControl {
  29. [self setSelectedViewControllerIndex:segmentedControl.selectedSegmentIndex];
  30. }
  31. - (void)createContentView {
  32. _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height - 50)];
  33. _contentView.backgroundColor = [UIColor grayColor];
  34. [self.view addSubview:_contentView];
  35. }
  36. - (void)setSelectedViewControllerIndex:(NSUInteger)index {
  37. UIViewController<RNNRootViewProtocol> *toVC = _viewControllers[index];
  38. [_contentView addSubview:toVC.view];
  39. [_currentViewController.view removeFromSuperview];
  40. _currentViewController = toVC;
  41. }
  42. - (void)setViewControllers:(NSArray *)viewControllers {
  43. _viewControllers = viewControllers;
  44. for (RNNRootViewController* childVc in viewControllers) {
  45. [childVc.view setFrame:_contentView.bounds];
  46. [childVc applyTopTabsOptions];
  47. }
  48. [self setSelectedViewControllerIndex:0];
  49. }
  50. - (void)viewController:(UIViewController*)vc changedTitle:(NSString*)title {
  51. NSUInteger vcIndex = [_viewControllers indexOfObject:vc];
  52. [_segmentedControl setTitle:title atIndex:vcIndex];
  53. }
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. }
  57. #pragma mark RNNRootViewProtocol
  58. - (UIViewController *)getLeafViewController {
  59. return _currentViewController;
  60. }
  61. @end