繁体   English   中英

以自定义模式呈现UINavigationController

[英]Presenting a UINavigationController in a custom modal

我正在尝试从UINavigationController呈现UINavigationController。 我正在使用新的iOS 7 transitioningDelegate东西,并且效果很好。...除了导航栏从高处开始,然后在动画结束时缩小。 我假设它缩小是因为条形图没有触及屏幕的顶部,但是为什么它从高开始呢? 我可以预防吗?

注意酒吧开始高

这是动画代码:

- (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];
    }];
}

我有解决这个问题的方法。

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

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

 - (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点的方法,因为这将涉及欺骗导航控制器以使其与窗口顶部相邻。

暂无
暂无

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

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