在APP中經(jīng)常有一個(gè)導(dǎo)航欄下面有很多類型的界面页藻。如果只是數(shù)據(jù)不同软免,界面大體相同很簡(jiǎn)單周循,刷新就可强法。但是如果界面不一致,就會(huì)有些麻煩湾笛。這時(shí)候用addchildviewcontroller就可以了饮怯。
蘋果新的API增加了addChildViewController方法,并且希望我們?cè)谑褂胊ddSubview時(shí)嚎研,同時(shí)調(diào)用[self addChildViewController:child]方法將sub view對(duì)應(yīng)的viewController也加到當(dāng)前ViewController的管理中蓖墅。
對(duì)于那些當(dāng)前暫時(shí)不需要顯示的subview,只通過addChildViewController把subViewController加進(jìn)去临扮;需要顯示時(shí)再調(diào)用transitionFromViewController方法论矾。將其添加進(jìn)入底層的ViewController中。
這樣做的好處:
1.無疑杆勇,對(duì)頁面中的邏輯更加分明了贪壳。相應(yīng)的View對(duì)應(yīng)相應(yīng)的ViewController。
2.當(dāng)某個(gè)子View沒有顯示時(shí)蚜退,將不會(huì)被Load闰靴,減少了內(nèi)存的使用。
3.當(dāng)內(nèi)存緊張時(shí)关霸,沒有Load的View將被首先釋放传黄,優(yōu)化了程序的內(nèi)存釋放機(jī)制。
直接上代碼:
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"仿網(wǎng)易導(dǎo)航欄";
self.headArray= @[@"11",@"22",@"33"];
[self addHeadView];
[self addChildVC];
}- (void)addHeadView {//? 不加文字scrollview 文字不顯示self.automaticallyAdjustsScrollViewInsets=NO;
self.headScrollView= [[UIScrollView alloc] initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,40)];
self.headScrollView.backgroundColor=[UIColor purpleColor];
self.headScrollView.showsVerticalScrollIndicator=NO;
self.headScrollView.contentSize= CGSizeMake(560,0);
self.headScrollView.bounces=NO;
self.headScrollView.pagingEnabled=YES;
[self.view addSubview:self.headScrollView];for(inti =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];
}
}- (void)addChildVC {
self.firstVC=[[ChildViewController alloc] init];
self.firstVC.view.backgroundColor=[UIColor redColor];
[self.firstVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];
[self addChildViewController:_firstVC];//addChildViewController 會(huì)調(diào)用 [child willMoveToParentViewController:self] 方法队寇,但是不會(huì)調(diào)用 didMoveToParentViewController:方法膘掰,官方建議顯示調(diào)用[self.firstVC didMoveToParentViewController:self];//默認(rèn),第一個(gè)視圖(你會(huì)發(fā)現(xiàn),全程就這一個(gè)用了addSubview)[self.view addSubview:self.firstVC.view];
self.currentVC=self.firstVC;
self.secondVC=[[ChildViewController alloc] init];
self.secondVC.view.backgroundColor=[UIColor greenColor];
[self.secondVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];
self.thirdVC=[[ChildViewController alloc] init];
self.thirdVC.view.backgroundColor=[UIColor blueColor];
[self.thirdVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];
}- (void)didClickHeadButtonAction:(UIButton *)button {//展示2個(gè),其余一樣,自行補(bǔ)全噢switch(button.tag) {case100:
[self replaceController:self.currentVC newController:self.firstVC];break;case101:
[self replaceController:self.currentVC newController:self.secondVC];break;case102:
[self replaceController:self.currentVC newController:self.thirdVC];break;default:break;
}
}//切換各個(gè)標(biāo)簽內(nèi)容- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController {/**
*? ? ? ? ? ? 著重介紹一下它
*? transitionFromViewController:toViewController:duration:options:animations:completion:
*? fromViewController? ? ? 當(dāng)前顯示在父視圖控制器中的子視圖控制器
*? toViewController? ? ? ? 將要顯示的姿勢(shì)圖控制器
*? duration? ? ? ? ? ? ? ? 動(dòng)畫時(shí)間(這個(gè)屬性,old friend 了 O(∩_∩)O)
*? options? ? ? ? ? ? ? ? 動(dòng)畫效果(漸變,從下往上等等,具體查看API)
*? animations? ? ? ? ? ? ? 轉(zhuǎn)換過程中得動(dòng)畫
*? completion? ? ? ? ? ? ? 轉(zhuǎn)換完成*/[self addChildViewController:newController];
[self transitionFromViewController:oldController toViewController:newController duration:2.0options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {if(finished) {//移除oldController,但在removeFromParentViewController:方法前不會(huì)調(diào)用willMoveToParentViewController:nil 方法佳遣,所以需要顯示調(diào)用[newController didMoveToParentViewController:self];
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
self.currentVC=newController;
}else{
self.currentVC=oldController;
}
}];
}//上面的代碼實(shí)現(xiàn)后效果類似于網(wǎng)易新聞界面识埋。但是也有一個(gè)界面上有兩種以上的類型頁面。這時(shí)也可以用這個(gè)實(shí)現(xiàn)零渐。- (void)addChildVC {
UIViewController FirstVC* first =[[FirstVC alloc] init];
[self addChildViewController:first];//addChildViewController 會(huì)調(diào)用 [child willMoveToParentViewController:self] 方法窒舟,但是不會(huì)調(diào)用 didMoveToParentViewController:方法,官方建議顯示調(diào)用[first didMoveToParentViewController:self];
[first.view setFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), width,300)];
[self.view addSubview:first.view];
SecondVC* second =[[SecondVC alloc] init];
[self addChildViewController:second];
[second didMoveToParentViewController:self];
[second.view setFrame:CGRectMake(0,CGRectGetMaxY(first.view.frame), width,300)];
[self.view addSubview:second.view];
}
這就實(shí)現(xiàn)了上下兩個(gè)子界面了诵盼。
補(bǔ)充:需要注意的地方
1. 容器controller最好定義一個(gè)專門用來展示子controller相關(guān)view的區(qū)域惠豺。我的代碼中沒有银还,畢竟這只是一個(gè)Demo。而且如果你定義了一個(gè)view后洁墙,masksToBounds很重要,要不然,整個(gè)controller都會(huì)被展示出來的.
2. 調(diào)用完addChildViewController之后還需要調(diào)用didMoveToParentViewController