在自定义模式中呈现 UINavigationController [英] Presenting a UINavigationController in a custom modal

查看:14
本文介绍了在自定义模式中呈现 UINavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 UINavigationController 呈现 UINavigationController.我正在使用新的 iOS 7 transitioningDelegate 东西,它工作得很好......除了导航栏开始很高,然后在动画结束时缩小.我假设它会缩小,因为条形图没有触及屏幕顶部,但为什么它一开始就很高?我可以防止这种情况吗?

I'm trying to present a UINavigationController from a UINavigationController. I'm using the new iOS 7 transitioningDelegate stuff and it's working great.... except the navigation bar starts out tall and then shrinks when the animation is over. I'm assuming it shrinks because the bar doesn't touch the top of the screen, but why does it start out tall? Can I prevent this?

这是动画代码:

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView* containerView = [transitionContext containerView];

    if (toViewController.isBeingPresented) {
        [self presentModalView:toViewController.view fromView:fromViewController.view inContainerView:containerView withTransitionContext:transitionContext];
    } else {
        [self dismissModalView:fromViewController.view fromView:toViewController.view withTransitionContext:transitionContext];
    }
}

- (void)presentModalView:(UIView*)modalView fromView:(UIView*)presentationView inContainerView:(UIView*)containerView withTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext {
    [containerView addSubview:modalView];
    presentationView.userInteractionEnabled = NO;

    modalView.layer.cornerRadius = 5;
    modalView.clipsToBounds = YES;

    CGRect finalFrame = CGRectInset(presentationView.frame, 10, 20);
    modalView.frame = CGRectOffset(finalFrame, 0, CGRectGetHeight(presentationView.frame));

    [UIView animateWithDuration:animationDuration animations:^{
        modalView.frame = finalFrame;
        presentationView.alpha = 0.2;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

推荐答案

我有解决这个问题的办法.

I have a solution to this problem.

来自 http://blog.jaredsinclair.com/post/61507315630/wrestling-with-status-bars-and-navigation-bars-on-ios-7,第 4 号:

UINavigationController 将根据一组相当奇怪且未记录的约束将其 UINavigationBar 的高度更改为 44 点或 64 点.如果 UINavigationController 检测到其视图框架的顶部与其 UIWindow 的顶部在视觉上是连续的,则它会绘制高度为 64 磅的导航栏.如果其视图的顶部与 UIWindow 的顶部不连续(即使仅相差一个点),则它以传统"方式绘制其导航栏,高度为 44 点.这个逻辑由 UINavigationController 执行,即使它是应用程序的视图控制器层次结构中的几个子级.没有办法阻止这种行为.

UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points, depending on a rather strange and undocumented set of constraints. If the UINavigationController detects that the top of its view’s frame is visually contiguous with its UIWindow’s top, then it draws its navigation bar with a height of 64 points. If its view’s top is not contiguous with the UIWindow’s top (even if off by only one point), then it draws its navigation bar in the "traditional" way with a height of 44 points. This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. There is no way to prevent this behavior.

因此,您的问题是 UINavigationController 在动画开始时检测到其视图的框架与 UIWindow 的顶部是连续的.这是因为当您将 modalView 添加为 containerView 的子视图时,modalView 的框架为 {0,0,0,0}.因为这个框架在视觉上与 UIWindow 的顶部是连续的,所以 UINavigationController 用 64 磅的高度绘制了它的 UINavigationBar.动画完成后,导航控制器的新框架不再与窗口顶部连续,它绘制高度为 44 的导航栏.

Your problem, therefore, is that the UINavigationController is detecting that its view's frame is contiguous with the UIWindow's top at the beginning of the animation. This is due to the fact that when you added the modalView as a subview of the containerView, the frame of the modalView is {0,0,0,0}. Because this frame would be visually contiguous with the UIWindow's top, the UINavigationController draws its UINavigationBar with a height of 64 points. When the animation is completed the new frame of the navigation controller is no longer continuous with the windows top, and it draws the navigation bar with a height of 44.

一种解决方案是在设置框架后将导航控制器的视图添加到容器视图中.像这样,

One solution is to add the navigation controller's view to the container view AFTER setting it's frame. Like so,

 - (void)presentModalView:(UIView*)modalView fromView:(UIView*)presentationView inContainerView:(UIView*)containerView withTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext {
    presentationView.userInteractionEnabled = NO;

    modalView.layer.cornerRadius = 5;
    modalView.clipsToBounds = YES;

    CGRect finalFrame = CGRectInset(presentationView.frame, 10, 20);
    modalView.frame = CGRectOffset(finalFrame, 0, CGRectGetHeight(presentationView.frame));
    [containerView addSubview:modalView];

    [UIView animateWithDuration:animationDuration animations:^{
        modalView.frame = finalFrame;
        presentationView.alpha = 0.2;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

这应该可以解决您的问题,因为导航栏在整个动画中将始终以 44 磅的高度绘制.我不知道有什么方法可以将导航栏的高度保持在 64 磅,因为这会导致导航控制器误以为它与窗口的顶部是连续的.

That should solve your problem, in that the navigation bar will always be drawn with a height of 44 points throughout the animation. I don't know of a way to keep the height of the navigation bar 64 points, because that would involve fooling the navigation controller into thinking it was contiguous with the top of the window.

这篇关于在自定义模式中呈现 UINavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆