react-native-navigation的迁移库

RNNTopTabsViewController.m 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. - (NSString *)componentId {
  59. return _currentViewController.componentId;
  60. }
  61. @end