我有一个使用PKRevealController的应用程序,它实现了一个滑出菜单,类似于 iOS 上流行的 Facebook 和 GMAIL 应用程序中的菜单。该应用程序是在 XCode 5 中构建的,并在 iOS 6 和 iOS 7 上运行。我需要弄清楚如何让它在这两个地方都正常工作,所以一个简单的 .XIB hack 让它在 iOS 7 中看起来不错,但让它看起来在 iOS 6 中更糟糕的是不行。
该代码适用于 iOS 6,其中状态栏是不透明的,并且顶部视图没有与状态栏混合 alpha。
但是,例如,在 iOS 7 上,我在我的 .xib 文件中创建了这个视图,下面是它在 ios 6 模拟器中运行时的显示方式,此处显示的是打开滑出菜单:
在 ios 7 上运行的相同 .xib 文件,当滑出菜单打开时,滑出菜单的 .xib 内容的顶部现在位于状态栏下方,正如 Apple 在其 ios 7 过渡指南中所说的那样:
我需要在 PKRevealController 中修改的类可能是正在创建和呈现包含视图的呈现视图控制器PKRevealControllerContainerView
,我认为包含的视图被称为。我想我可能需要像这样创建某种视图层次结构:
[ Outermost View container
[ some kind of blob to occupy the header area ]
[ the client view I want to appear the way it did in iOS 6]
]
我一直在阅读,可能有更简单的方法,但我不太了解它们,比如向我的 info.plist 添加属性的方法,比如View controller-based status bar appearance
= YES
。我试过它没有达到预期的效果。
我该如何解决这个问题?我已经阅读了Apple 发布的Fine Guide,它没有提供代码,只有像状态栏上的这个页面这样的一般指导。
复制这个问题很容易,只需克隆 git repo https://github.com/pkluz/PKRevealController
,构建并运行。
弹出弹出视图的代码如下所示:
- (void)addLeftViewControllerToHierarchy
{
if (self.leftViewController != nil && ![self.childViewControllers containsObject:self.leftViewController])
{
[self addChildViewController:self.leftViewController];
self.leftViewContainer.viewController = self.leftViewController;
if (self.leftViewContainer == nil)
{
self.leftViewContainer = [[PKRevealControllerContainerView alloc] initForController:self.leftViewController shadow:NO];
self.leftViewContainer.autoresizingMask = [self autoresizingMaskForLeftViewContainer];
}
self.leftViewContainer.frame = [self leftViewFrame];
[self.view insertSubview:self.leftViewContainer belowSubview:self.frontViewContainer];
[self.leftViewController didMoveToParentViewController:self];
}
}
上面是由 PKRevealController.m 调用的,如下所示:
- (void)showLeftViewControllerAnimated:(BOOL)animated
completion:(PKDefaultCompletionHandler)completion
{
__weak PKRevealController *weakSelf = self;
void (^showLeftViewBlock)(BOOL finished) = ^(BOOL finished)
{
[weakSelf removeRightViewControllerFromHierarchy];
[weakSelf addLeftViewControllerToHierarchy]; // HELLO LEFT Slide-out menu.
....
有比我的想法更好的方法吗?Apple 是否提供了某种方法来简化此操作,或者尝试在单个代码库中支持 iOS 6 和 iOS 7 是否让我做上述我正在考虑的黑客行为?
例如,这里是一个非常丑陋的 hack,我不费心在苹果系统状态栏下方放置任何视图,在顶部留下一个黑条,这不好,但它表明我正在修改正确的区域代码,至少:
- (void)addLeftViewControllerToHierarchy
{
CGRect lvFrame;
if (self.leftViewController != nil && ![self.childViewControllers containsObject:self.leftViewController])
{
[self addChildViewController:self.leftViewController];
self.leftViewContainer.viewController = self.leftViewController;
if (self.leftViewContainer == nil)
{
self.leftViewContainer = [[PKRevealControllerContainerView alloc] initForController:self.leftViewController shadow:NO];
self.leftViewContainer.autoresizingMask = [self autoresizingMaskForLeftViewContainer];
}
lvFrame = [self leftViewFrame];
lvFrame.origin.y += 20; // ugly hack demo code only! don't really do it this badly!
lvFrame.size.height -= 20;
self.leftViewContainer.frame = lvFrame;
[self.view insertSubview:self.leftViewContainer belowSubview:self.frontViewContainer];
[self.leftViewController didMoveToParentViewController:self];
}
}
如果我还将其添加到UIViewController+PKRevealController.m
:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleBlackOpaque;
}
添加上述代码时,会导致以下提示/警告:
Category is implementing a method that will also be implemented by its primary class.
我将上述注释包括在内以展示我的尝试,我欢迎了解真正的专家是如何做到这一点的。
我自己修改的 PKRevealController 代码副本,包括上面的 hack,以稍微改进的形式,可以在这里找到: https://github.com/wpostma/PKRevealController