我如何“合法"淡出应用程序中的导航栏? [英] How do I "legally" fade out the navigation bar in an app?

查看:18
本文介绍了我如何“合法"淡出应用程序中的导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎删除带有动画的导航栏的唯一方法是向上滑动.我希望它褪色,就像在 Photos.app 中一样.

It seems the only way to remove a navigation bar with animation is via it sliding upward. I want it to fade, like in Photos.app.

更改 alpha 最简单,但是 Apple 的指南状态:

It would be easiest to change the alpha, however Apple's guidelines state:

在 iOS v5.0 之前,与导航一起使用时控制器,您只能进行少数直接自定义进入导航栏.具体可以修改barStyle、tintColor 和 translucent 属性,但绝不能直接更改 UIView 级别的属性,例如框架、边界、alpha,或者直接隐藏属性.

Prior to iOS v5.0, when used in conjunction with a navigation controller, there are only a handful of direct customizations you can make to the navigation bar. Specifically, it is alright to modify the barStyle, tintColor, and translucent properties, but you must never directly change UIView-level properties such as the frame, bounds, alpha, or hidden properties directly.

该语言有点奇怪,就像它在 iOS 5 之前的声明一样,但它声明你不能直接更改 alpha 值,而且它从来没有声明你现在可以这样做.

The language is a little weird, as it states prior to iOS 5, but it stated you're not allowed to change the alpha value directly, and it never states you're allowed to now.

我不希望我的应用被拒绝.

I don't want my app to get rejected.

如何像淡出状态栏一样淡出导航栏?

How do I fade out the navigation bar like I would the status bar?

推荐答案

这是一个有趣的解决方案.

This was a fun one to solve.

应该很简单:使用 UIView transitionWithView 在导航栏的隐藏和非隐藏状态之间执行交叉淡入淡出,由公共 API 设置setNavigationBarHidden:动画:.事实上,这适用于淡出"导航栏,但将其淡入会出现问题.问题是导航栏会滑动到位,无论 UIView +transitionWithView: 不会为动画属性(例如 frame)设置动画,除非您指定 UIViewAnimationOptionAllowAnimatedContent.

It should have been very straight-forward: use UIView transitionWithView to perform a cross-fade between the hidden and non-hidden states of the navigation bar, as set by the public API setNavigationBarHidden:animated:. In fact this works for "fading out" the navbar, but fading it back in had an issue. The issue was that the navbar would slide into place regardless of the fact that UIView +transitionWithView: doesn't animate animatable properties (e.g. frame) unless you specify UIViewAnimationOptionAllowAnimatedContent.

对我来说,这说明 UINavigationController 在内部将 UINavigationBar 重新定位在动画块内,无论是否在调用 setNavigationBarHidden:animated:.当 animate: 设置为 NO 时,此动画块的持续时间可能设置为0".

To me this says that internally the UINavigationController repositions the UINavigationBar inside an animation block regardless of whether animating was specified in the call to setNavigationBarHidden:animated:. The duration for this animation block is probably set to '0' when animate: is set to NO.

解决方案是在交叉淡入淡出过渡之前设置导航栏可见(无动画).这样可以确保导航栏在正确的位置开始淡入淡出,并且淡入淡出只会显示新的非隐藏状态.

The solution is to set the navigation bar visible (sans animation) before the cross-fade transition. This ensures that the navigation bar begins the cross-fade in the correct position, and that the cross-fade will only reveal the new non-hidden state.

我的示例项目是一个标准的单视图应用程序.故事板上有一个 UINavigationController,它是入口点.我将此控制器的 UINavigationBar 的条形样式设置为黑色半透明(类似于照片应用程序).导航控制器的 rootViewController 是一个简单的 UIViewController,其中 UIImageView 填充了整个边界(也像 Photos 应用程序).我在视图上添加了一个 UITapGestureRecognizer 以在视图控制器中调用以下代码:

My sample project is a standard Single View Application. On the storyboard is a UINavigationController, which is the entry point. I set the bar style for this controller's UINavigationBar to black-translucent (similar to the Photos app). The navigation controller's rootViewController is a simple UIViewController with a UIImageView filling the entire bounds (also like the Photos app). I added a UITapGestureRecognizer on the view to invoke the following code, in the view controller:

- (IBAction) onShowHideNavbar: (id) sender
{
    BOOL hide = !self.navigationController.navigationBarHidden;

    if ( !hide)
    {
        [self.navigationController setNavigationBarHidden: hide animated: NO];
    }

    [UIView transitionWithView: self.navigationController.view
                      duration: 1
                       options: UIViewAnimationOptionTransitionCrossDissolve
                    animations: ^{

                        [self.navigationController setNavigationBarHidden: hide animated: NO];
                    }
                    completion: nil ];
}

所有这一切,我认为您不会因为直接弄乱 UINavigationBar 的隐藏或 alpha 属性而遇到任何麻烦(Apple 拒绝).文档警告不要触摸这些,因为它们由 UINavigationController 管理,更改它们可能会产生看不见的后果.但在我看来,它们是公共 API,因此使用它们不应成为拒绝的理由.

All this said, I don't think you'd get into any trouble (Apple Rejection) for messing with the hidden or alpha properties of the UINavigationBar directly. The docs warn against touching these because they're managed by the UINavigationController and changing them might have unseen consequences. But In My Opinion they're public APIs and as such using them shouldn't be cause for rejection.

这篇关于我如何“合法"淡出应用程序中的导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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