我在导航控制器中嵌入了一个控制器。假设我有一个按钮可以重新定位self.navigationController.navigationBar。然后我用任何控制器进行presentViewControllerAnimated(无论是否导航都没关系)并且在解除之后导航栏返回到它的原始位置(实际上它在解除动画开始时处于其原始位置)。在iOS 6及更早版本中,该栏不会自动重新定位。任何想法如何在iOS 7中阻止这种重新定位?
答案 0 :(得分:1)
好的,所以我终于做对了。
首先 - Apple不希望我们改变UINavigationBar的位置。因此,您应该不惜一切代价避免它。在我的情况下,我有一个应用程序来修复它移动UINavigationBar显示滑出菜单。滑出菜单问题的正确解决方案是将UINavigationController 放在中 - 然后你可以用它的内容(无论它是什么)滑动整个UINavigationController,一切正常。出于某种原因,UINavigationController在这个应用程序之外。所以,我不得不诉诸黑客。如果您有任何选项不使用它,请不要使用此方法。这是一个黑客攻击,它可能会在更多的iOS版本中破解,Apple肯定不会欣赏它。
首先,探索iOS7中的新过渡系统:http://www.doubleencore.com/2013/09/ios-7-custom-transitions/
然后,替换:
[self presentViewController:navigationController animated:YES completion:nil];
带
if([UIApplication iOS7]) /* or any other custom iOS7 detection method you implement */
{ /* we simulate old transition with nav bar slided out */
navigationController.transitioningDelegate = [OMModalTransitionDelegate new];
}
[self presentViewController:navigationController animated:YES completion:nil];
因此,我们需要一个转换委托来模拟标准行为并执行技巧。
#import "OMModalTransitionDelegate.h"
#import "OMAnimatedTransitioning.h"
@implementation OMModalTransitionDelegate
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
OMAnimatedTransitioning *transitioning = [OMAnimatedTransitioning new];
return transitioning;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
OMAnimatedTransitioning *transitioning = [OMAnimatedTransitioning new];
transitioning.reverse = YES;
return transitioning;
}
@end
现在是实际的动画管理器(你必须自己在UINavigationBar的类别中实现sharedBar):
static NSTimeInterval const DEAnimatedTransitionDuration = 0.4f;
static NSTimeInterval const DEAnimatedTransitionMarcoDuration = 0.15f;
@implementation OMAnimatedTransitioning
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *container = [transitionContext containerView];
UIView *superView = [UINavigationBar sharedBar].superview;
CGRect barFrame = [UINavigationBar sharedBar].frame;
if(self.reverse)
{ /* Trick - first, remove the bar from it's superview before animation starts */
[[UINavigationBar sharedBar] removeFromSuperview];
}
CGRect oldFrame = container.bounds;
if (self.reverse)
{
[container insertSubview:toViewController.view belowSubview:fromViewController.view];
}
else
{
toViewController.view.frame = oldFrame;
toViewController.view.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(oldFrame));
[container addSubview:toViewController.view];
}
[UIView animateKeyframesWithDuration:DEAnimatedTransitionDuration delay:0 options:0 animations:^
{
if (self.reverse)
{
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(oldFrame));
double delayInSeconds = 0.01; /* Trick - after an imperceivable delay - add it back - now it is immune to whatever Apple put there to move it */
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
{
[UINavigationBar sharedBar].frame = barFrame;
[superView addSubview:[UINavigationBar sharedBar]];
});
}
else
{
toViewController.view.transform = CGAffineTransformIdentity;
}
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return DEAnimatedTransitionDuration;
}
@end
答案 1 :(得分:0)
在自定义导航控制器中,添加
- (void)viewWillLayoutSubviews {
//do your navigation bar layout
}
希望这可以帮到你。提醒一下,上面的方法只支持ios&gt; = 5.0。