[英]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];
}];
}
我有解決這個問題的方法。
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.