Add child view controller to UINavigationController
2015-12-18 17:48
1641 查看
http://stackoverflow.com/questions/19257999/add-child-view-controller-to-uinavigationcontroller I'm trying to add a child view controller to a UIViewControllercontained in a UINavigationControllerwith this code: - (void)buttonTapped:(id)sender { MyChildController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyChild"]; [self addChildViewController:viewController]; [self.view addSubview:viewController.view]; [viewController didMoveToParentViewController:self]; viewController.view.alpha = 0.0f; [UIView animateWithDuration:0.4 animations:^{ viewController.view.alpha = 1.0f; }]; } But this is the result: As you can see the UINavigatioBarand the UIToolbarare still on top of the child view controller. How can I put the child view controller on top of all? I've already tried to replace the code with: [self.navigationController addChildViewController:viewController]; [self.navigationController.view addSubview:viewController.view]; [viewController didMoveToParentViewController:self.navigationController]; But in this way the viewDidAppear:animatedof the viewControllerdoesn't get called, dunno why. iphone objective-c uiviewcontroller ios7 childviewcontroller
| |||
add a comment |
2 Answers
activeoldestvotesup vote5down voteaccepted | In your first view controller, do something like this:- (IBAction)buttonClick:(id)sender { SecondViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; UIImage *blurryImage = [UIImage imageNamed:@"foo.jpeg"]; secondView.imageView.image = blurryImage; [self.navigationController addChildViewController:secondView]; secondView.view.frame = self.navigationController.view.frame; [self.navigationController.view addSubview:secondView.view]; } Then in your second view controller, add the getter for your imageview: -(UIImageView *)imageView { if( _imageView == nil ) { _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 548)]; [self.view addSubview:_imageView]; } return _imageView; }
| ||||||||||||||||||||
comments |
up vote14down vote | @Sam's comment is correct. You need to call beginApperanceTransition:animated:and endAppearanceTransitionfor viewDidAppearto be triggered. The reason why UINavigationControllerdoes not call viewDidAppearwhen you add a child view controller is because it has overridden its container composition methods to prevent the programmer from adding a child view controller in strange places. In your case, it doesn't want your child view to cover up the navigation bar. The correct usage of a navigation controller is to have children appear under the navigation bar. Nonetheless, you can still force upon this non-standard UI by manually telling the child when it is appearing and when it has finished appearing.
|
shareimprove this answer | edited Jul 26 at 14:14 kernix 8861831 | answered Mar 25 '14 at 21:19 Pwner 1,19211643 |
| I think child be calling didMoveToParentViewController:when its being removed.. Something like, [child didMoveToParentViewController:nil]– Mohammad Abdurraafay May 11 '14 at 8:37 | ||
It helped me ... thank you. Gave upvote Pwner. – Meenu Jul 29 '14 at 12:43 | |||
very helpful, thanks! – kernix Jul 26 at 12:48 |
a comment
相关文章推荐
- 自动化测试: sikuli,一个基于界面图像的gui测试框架
- 【原创】在NGUI使用图片文字(数字、美术字)(BMFont)
- 使用UIImagePickerController时3DTouch引起的Crash问题的解决
- UIViewController的生命周期笔记 by STP
- UIImage的等比例压缩和缩放实现
- UIPageViewController 专辑
- UIViewController 专辑
- ios 长按手势UILongPressGestureRecognizer 开始/结束(按下/抬起)
- Android UI模板设计--自定义actionbar(topbar)模板(慕课网学习笔记)
- Druid :大数据实时处理的开源分布式系统(1)
- break & continue
- Druid :大数据实时处理的开源分布式系统(1)
- Codeforces Round #313 (Div. 2) 560D Equivalent Strings(dos)
- Uiautomator——API详解
- processing-MySQL learning1-getString() , getInt() and query()
- iPhone开发关于UDID和UUID的一些理解
- ZOJ 题目3911 Prime Query(线段树求区间素数个数)
- 01.EasyUI加入项目
- UITableView
- UITableView出现的错误