原本只是打算介紹一下addChildViewController這個方法的,正好今天朋友去換工做面試問到網易新聞標籤欄效果的實現,就結合它,用個小Demo實例介紹一下:(具體解釋都寫在了Demo裏面的註釋)面試
-
- #import "HMTMainViewController.h"
- #import "HMTFirstViewController.h"
- #import "HMTSecondViewController.h"
- #import "HMTThirdViewController.h"
-
- @interface HMTMainViewController () <UIScrollViewDelegate>
-
- @property (nonatomic ,strong) HMTThirdViewController *thirdVC;
- @property (nonatomic ,strong) HMTFirstViewController *firstVC;
- @property (nonatomic ,strong) HMTSecondViewController *secondVC;
-
- @property (nonatomic ,strong) UIViewController *currentVC;
-
- @property (nonatomic ,strong) UIScrollView *headScrollView;
-
- @property (nonatomic ,strong) NSArray *headArray;
-
- @end
-
- @implementation HMTMainViewController
-
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
-
- }
- return self;
- }
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- self.navigationItem.title = @"網易新聞Demo";
-
- self.headArray = @[@"頭條",@"娛樂",@"體育",@"財經",@"科技",@"NBA",@"手機"];
-
- self.automaticallyAdjustsScrollViewInsets = NO;
- self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
- self.headScrollView.backgroundColor = [UIColor purpleColor];
- self.headScrollView.contentSize = CGSizeMake(560, 0);
- self.headScrollView.bounces = NO;
- self.headScrollView.pagingEnabled = YES;
- [self.view addSubview:self.headScrollView];
- for (int i = 0; i < [self.headArray count]; i++) {
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
- button.frame = CGRectMake(0 + i*80, 0, 80, 40);
- [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
- button.tag = i + 100;
- [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.headScrollView addSubview:button];
-
- }
-
-
-
-
- self.firstVC = [[HMTFirstViewController alloc] init];
- [self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
- [self addChildViewController:_firstVC];
-
- self.secondVC = [[HMTSecondViewController alloc] init];
- [self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
-
- self.thirdVC = [[HMTThirdViewController alloc] init];
- [self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
-
-
- [self.view addSubview:self.firstVC.view];
- self.currentVC = self.firstVC;
-
- }
-
- - (void)didClickHeadButtonAction:(UIButton *)button
- {
-
- if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
- return;
- }else{
-
-
- switch (button.tag) {
- case 100:
- [self replaceController:self.currentVC newController:self.firstVC];
- break;
- case 101:
- [self replaceController:self.currentVC newController:self.secondVC];
- break;
- case 102:
-
- break;
- case 103:
-
- break;
- case 104:
-
- break;
- case 105:
-
- break;
- case 106:
-
- break;
-
- default:
- break;
- }
- }
-
- }
-
- - (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
- {
-
-
- [self addChildViewController:newController];
- [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
-
- if (finished) {
-
- [newController didMoveToParentViewController:self];
- [oldController willMoveToParentViewController:nil];
- [oldController removeFromParentViewController];
- self.currentVC = newController;
-
- }else{
-
- self.currentVC = oldController;
-
- }
- }];
- }