如何在tabBarController上方显示一半modalView?

时间:2015-03-24 09:08:50

标签: ios navigationcontroller tabbarcontroller

我正在使用tabBarController和navigationController 我正在TabBarController的一个viewControllers上显示一个Half ModalView作为孩子。 注意我正在使用iPhone-5模拟器。我的问题是topViewController的高度是504,即使它应该是568我无法确定在tabBar上方显示它的正确位置。

UIViewController *viewCon =  [(UINavigationController*)[tabBarController selectedViewController] topViewController];

     UIViewController*vc=[tabBarController.viewControllers objectAtIndex:0];
        NSLog(@"vc,%f",vc.view.frame.size.height);// prints 568

        if (viewCon.childViewControllers.count == 0) {

        UIStoryboard* story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        _modal = [story instantiateViewControllerWithIdentifier:@"HalfModal"];
        [viewCon addChildViewController:_modal];

        _modal.view.frame = CGRectMake(0, screenHeight ,screenWidth, screenHeight);

        NSLog(@"TopviewController height ,%f",CGRectGetHeight(viewCon.view.frame));//prints 504

        frame = CGRectMake(0, 0, screenWidth, screenHeight);
        _view2 = [[UIView alloc]initWithFrame:frame];

        [viewCon.view addSubview:_view2];
        [viewCon.view addSubview:_modal.view];

        [UIView animateWithDuration:0.5
                         animations:^{
                             [_view2 setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];                             

                             _modal.view.frame = CGRectMake(0, screenHeight/2, 320, 284);

                         }
                         completion:^(BOOL finished) {
                             [_modal didMoveToParentViewController:viewCon];
                         }];

1 个答案:

答案 0 :(得分:2)

首先,我建议避免这样的事情

CGRectMake(0, screenHeight/2, 320, 284);

(设置大小/元素位置的常量),您必须从UIScreen对象计算它。

CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width

第二:如果你想显示最顶层的视图,你可以尝试将它添加到UIWindow