我如何解决 hidesBottomBarWhenPushed 在 iOS 6 SDK 中表现得很奇怪? [英] How can I work around hidesBottomBarWhenPushed acting weird with the iOS 6 SDK?

查看:17
本文介绍了我如何解决 hidesBottomBarWhenPushed 在 iOS 6 SDK 中表现得很奇怪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了在这个 OpenRadar 问题中描述的相同问题.正如那里所说:

I ran into the same problem described in this OpenRadar issue. As stated there:

总结:UIViewController 的 hidesBottomBarWhenPushed 属性对于使用 iOS 6 SDK(不是测试版 SDK)构建的应用程序,无法按预期工作适用于 iOS 7).隐藏底栏时动画很奇怪(例如标签栏).

Summary: The hidesBottomBarWhenPushed property of UIViewController doesn't work as expected for apps built with iOS 6 SDK (not beta SDKs for iOS 7). The animation is weird when hiding the bottom bar (e.g. a tab bar).

重现步骤:

  1. 使用 Xcode 4 中的 TabBar 模板创建一个新项目.将 UINavigationController 添加到 FirstViewController.在上面添加一个按钮FirstViewController 并设置其操作以推送新的视图控制器.(请参阅随附的示例代码)

  1. Create a new project with the TabBar template in Xcode 4. Add a UINavigationController to the FirstViewController. Add a button on the FirstViewController and set its action to push a new view controller. (please see the sample code attached)

在 iOS 7 beta 5 设备上运行演示.

Run the demo on an iOS 7 beta 5 device.

按下按钮,从 UINavigationController 返回,注意动画视图转换.

Press the button, back from the UINavigationController, pay attention to the animated view transitions.

预期结果:动画效果与 iOS 6 完全相同设备.

Expected Results: The animation works exactly the same as on an iOS 6 device.

实际结果:动画看起来很奇怪.FirstViewController 是从底部向下滑动.

Actual Results: The animation looks weird. The FirstViewController is sliding down from the bottom.

示例代码:http://cl.ly/QgZZ

在使用 iOS 6 SDK 进行构建时,有什么方法可以解决或解决此问题?

Is there any way to fix or work around this when building with the iOS 6 SDK?

推荐答案

这个问题肯定存在.我做了一些调查并找出了导致它的原因.当使用 UINavigationController 推送视图控制器时,视图控制器的视图包含在 UIViewControllerWrapperView 中,这是一个由 UINavigationController 管理的私有 Apple 视图.当过渡动画即将发生并且 hidesBottomBarWhenPushed 设置为 YES 时,此 UIViewControllerWrapperView 正在使用 Y 轴的错误 position 进行动画处理,所以解决方案只是覆盖此行为并为动画提供正确的值.代码如下:

This issue definitely exists. I did some investigation and found out what's causing it. When pushing a view controller with UINavigationController, you view controller's view is contained in a UIViewControllerWrapperView, which a private Apple's view managed by the UINavigationController. When the transition animation is about to occur and the hidesBottomBarWhenPushed is set to YES, this UIViewControllerWrapperView is being animated with wrong position for the Y axis, so the solution is just to overwrite this behaviour and give correct values for the animation. Here's the code:

//Declare a property
@property (nonatomic, assign) BOOL shouldFixAnimation;

...

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

#ifndef __IPHONE_7_0 //If this constant is not defined then we probably build against lower SDK and we should do the fix
    if (self.hidesBottomBarWhenPushed && [[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && animated && self.navigationController) {
        self.shouldFixAnimation = YES;
    }
#endif

}

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

#ifndef __IPHONE_7_0
    if(self.shouldFixAnimation) {
        self.shouldFixAnimation = NO;
        CABasicAnimation *basic = (CABasicAnimation *)[self.view.superview.layer animationForKey:@"position"]; //The superview is this UIViewControllerWrapperView

        //Just in case for future changes from Apple
        if(!basic || ![basic isKindOfClass:[CABasicAnimation class]]) 
            return;

        if(![basic.fromValue isKindOfClass:[NSValue class]])
            return;

        CABasicAnimation *animation = [basic mutableCopy];

        CGPoint point = [basic.fromValue CGPointValue];

        point.y = self.view.superview.layer.position.y;

        animation.fromValue = [NSValue valueWithCGPoint:point];

        [self.view.superview.layer removeAnimationForKey:@"position"];
        [self.view.superview.layer addAnimation:animation forKey:@"position"];
    }
#endif

}

这篇关于我如何解决 hidesBottomBarWhenPushed 在 iOS 6 SDK 中表现得很奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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