简体   繁体   English

以自定义模式呈现UINavigationController

[英]Presenting a UINavigationController in a custom modal

I'm trying to present a UINavigationController from a UINavigationController. 我正在尝试从UINavigationController呈现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. 我正在使用新的iOS 7 transitioningDelegate东西,并且效果很好。...除了导航栏从高处开始,然后在动画结束时缩小。 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? 我可以预防吗?

注意酒吧开始高

Here's the animation code: 这是动画代码:

- (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. 我有解决这个问题的方法。

From http://blog.jaredsinclair.com/post/61507315630/wrestling-with-status-bars-and-navigation-bars-on-ios-7 , number 4: http://blog.jaredsinclair.com/post/61507315630/wrestling-with-status-bars-and-navigation-bars-on-ios-7编号4:

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. UINavigationController会将其UINavigationBar的高度更改为44点或64点,具体取决于一组相当奇怪且未记录的约束。 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. 如果UINavigationController检测到其视图框架的顶部在视觉上与其UIWindow的顶部连续,则它将绘制高度为64点的导航栏。 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. 如果其视图的顶部与UIWindow的顶部不连续(即使仅偏离一点),则它以“传统”方式绘制其导航栏,高度为44磅。 This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. 此逻辑由UINavigationController执行,即使它在应用程序的视图控制器层次结构中位于多个子级之下。 There is no way to prevent this behavior. 没有办法防止这种行为。

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. 因此,您的问题是UINavigationController正在检测其视图的框架与动画开始时UIWindow的顶部是连续的。 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}. 这是由于,当你添加的modalView作为一个子视图containerView ,该modalView的框架是{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. 因为此框架与UIWindow的顶部在视觉上是连续的,所以UINavigationController绘制其UINavigationBar的高度为64点。 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. 动画完成后,导航控制器的新框架不再与窗口顶部连续,并绘制高度为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];
    }];
}

That should solve your problem, in that the navigation bar will always be drawn with a height of 44 points throughout the animation. 那应该可以解决您的问题,因为在整个动画中始终以44点的高度绘制导航栏。 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. 我不知道将导航栏的高度保持在64点的方法,因为这将涉及欺骗导航控制器以使其与窗口顶部相邻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM